gpt4 book ai didi

javascript - 如何切换对每个 div 具有不同效果的多个 div

转载 作者:行者123 更新时间:2023-11-28 00:48:31 25 4
gpt4 key购买 nike

我有多个 div,每个都有自己的位置。我想要的是在单击 div 内的文本时切换每个 div 我是 Javascript 的新手,所以请多多包涵。我知道如何通过像下面那样切换一个 div 来做到这一点。

var para = document.querySelectorAll('.wrap div p');
for (var i = 0; i < para.length; i++) {
para[i].addEventListener('click', expand);
}

function expand(e) {
if ((e.target === para[2]) && (document.querySelector('.wrap #three').style.width === '15%')) {
document.querySelector('.wrap #three').style.width = '100%';
}else{
document.querySelector('.wrap #three').style.width = '15%';

}
}

我也尝试过使用 if else 语句,但它只是扩展了 div,我无法将它缩小到原来的位置。我知道有一种简单的方法可以实现这一点,但我想不出来。

var para = document.querySelectorAll('.wrap div p');
for (var i = 0; i < para.length; i++) {
para[i].addEventListener('click', expand);
}

function expand(e) {
if (e.target === para[0]) {
document.querySelector('.wrap #one').style.width = '100%';
} else if (e.target === para[1]) {
document.querySelector('.wrap #two').style.width = '100%';

} else if (e.target === para[2]) {
document.querySelector('.wrap #three').style.width = '100%';

} else if (e.target === para[3]) {
document.querySelector('.wrap #four').style.width = '100%';

} else if (e.target === para[4]) {
document.querySelector('.wrap #five').style.width = '100%';

} else if (e.target === para[5]) {
document.querySelector('.wrap #six').style.width = '100%';

} else if (e.target === para[6]) {
document.querySelector('.wrap #seven').style.width = '100%';

}
}
.wrap {
position: relative;
width: 100%;
height: 100vh;
background: white;
display: flex;
flex-direction: row;
}

.wrap div {
position: relative;
height: 100%;
overflow: hidden;
background: #fdfdfd;
transition: 0.2s ease-in;
box-shadow: 0 0 40px rgba(0, 0, 0, 0.7);
}

.wrap div:before {
content: '';
position: absolute;
width: 250px;
height: 250px;
right: -150px;
top: 27%;
background: red;
border-radius: 50%;
cursor: pointer;
}

.wrap p {
position: absolute;
top: 40%;
cursor: pointer;
transform: rotate(-90deg);
font-size: 25px;
font-weight: bold;
color: white;
font-family: 'Halvetica', sans-serif;
}

.wrap #one {
position: absolute;
width: 21%;
}

.wrap #one:before {
background: #FF1D55;
}

.wrap #one p {
right: -8px;
}

.wrap #two {
position: absolute;
width: 18%;
}

.wrap #two:before {
background: #FFE31A;
}

.wrap #two p {
right: -23px;
}

.wrap #three {
position: absolute;
width: 15%;
}

.wrap #three:before {
background: #00E2AA;
}

.wrap #three p {
right: 2px;
}

.wrap #four {
position: absolute;
width: 12%;
}

.wrap #four:before {
background: #B90CB4;
}

.wrap #four p {
right: -13px;
}

.wrap #five {
position: absolute;
width: 9%;
}

.wrap #five:before {
background: #FF3400;
}

.wrap #five p {
right: -5px;
}

.wrap #six {
position: absolute;
width: 6%;
}

.wrap #six:before {
background: #30B7E2;
}

.wrap #six p {
right: -18px;
}

.wrap #seven {
position: absolute;
width: 3%;
}

.wrap #seven:before {
background: #220054;
}

.wrap #seven p {
right: -12px;
}
<div class="wrap">
<div id="one">
<p>About</p>
</div>
<div id="two">
<p>Services</p>
</div>
<div id="three">
<p>Blog</p>
</div>
<div id="four">
<p>Follow</p>
</div>
<div id="five">
<p>Team</p>
</div>
<div id="six">
<p>Contact</p>
</div>
<div id="seven">
<p>History</p>
</div>
</div>

我希望我能把一切都解释清楚。任何帮助将不胜感激

最佳答案

使用 classList.add/remove 而不是通过 javascript 添加内联样式。在您的代码中,您将事件附加到占用很小区域的 p ,我添加了一个类 p 以便它的背景可见并且易于点击.

此外,您不需要定位每个元素并向其添加类。

在此示例中获取目标元素及其父元素和toggle

var para = document.querySelectorAll('.wrap div p');
for (let i = 0; i < para.length; i++) {

para[i].addEventListener('click', expand);
}

function expand(e) {

if (e.target.parentNode.classList.contains('fullWidth')) {
e.target.parentNode.classList.remove('fullWidth')
} else {
e.target.parentNode.classList.add('fullWidth')
}

}
.wrap {
position: relative;
width: 100%;
height: 100vh;
background: white;
display: flex;
flex-direction: row;
}

.wrap div {
position: relative;
height: 100%;
overflow: hidden;
background: #fdfdfd;
transition: 0.2s ease-in;
box-shadow: 0 0 40px rgba(0, 0, 0, 0.7);
}

.wrap div:before {
content: '';
position: absolute;
width: 250px;
height: 250px;
right: -150px;
top: 27%;
background: red;
border-radius: 50%;
cursor: pointer;
}

.wrap p {
position: absolute;
top: 40%;
cursor: pointer;
transform: rotate(-90deg);
font-size: 25px;
font-weight: bold;
color: white;
font-family: 'Halvetica', sans-serif;
}

.wrap #one {
position: absolute;
width: 21%;
}

.wrap #one:before {
background: #FF1D55;
}

.wrap #one p {
right: -8px;
}

.wrap #two {
position: absolute;
width: 18%;
}

.wrap #two:before {
background: #FFE31A;
}

.wrap #two p {
right: -23px;
}

.wrap #three {
position: absolute;
width: 15%;
}

.wrap #three:before {
background: #00E2AA;
}

.wrap #three p {
right: 2px;
}

.wrap #four {
position: absolute;
width: 12%;
}

.wrap #four:before {
background: #B90CB4;
}

.wrap #four p {
right: -13px;
}

.wrap #five {
position: absolute;
width: 9%;
}

.wrap #five:before {
background: #FF3400;
}

.wrap #five p {
right: -5px;
}

.wrap #six {
position: absolute;
width: 6%;
}

.wrap #six:before {
background: #30B7E2;
}

.wrap #six p {
right: -18px;
}

.wrap #seven {
position: absolute;
width: 3%;
}

.wrap #seven:before {
background: #220054;
}

.wrap #seven p {
right: -12px;
}

p {
background: greenyellow
}

.fullWidth {
width: 100% !important;
}
<div class="wrap">
<div id="one">
<p>About</p>
</div>
<div id="two">
<p>Services</p>
</div>
<div id="three">
<p>Blog</p>
</div>
<div id="four">
<p>Follow</p>
</div>
<div id="five">
<p>Team</p>
</div>
<div id="six">
<p>Contact</p>
</div>
<div id="seven">
<p>History</p>
</div>
</div>

关于javascript - 如何切换对每个 div 具有不同效果的多个 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53396480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com