gpt4 book ai didi

javascript - 在语言面板中切换图像(标志)

转载 作者:行者123 更新时间:2023-12-01 01:54:40 28 4
gpt4 key购买 nike

我有一个语言面板,其中有一个图像 (main-image),显示页面加载时的情况。我还有三个额外的图像,它们在页面加载时隐藏。

问题是当点击附加图像之一时如何切换主图像。我需要使用单击的 image 切换主图像。

这里是Codepen

我的第一次尝试

let menuBtn = document.getElementById("menu-btn");
menuBtn.addEventListener("click", ()=>{
let mainBtnImg = document.getElementById("main-btn-img");
let otherThreeImg = document.querySelectorAll(".menu-img");
for(let i = 0;i< otherThreeImg.length; i++){
mainBtnImg.src = otherThreeImg[this].src;
}
})

第二次尝试

仅更改了这部分

mainBtnImg.src = this.otherThreeImg;

反对者请先发表评论

$(document).on('click', function (e) {
if ($(e.target).closest(".menu-btn").length === 0) {
$('.menu-nav').removeClass('menu-nav--open');
}
});

$('.menu-btn').on('click', function(e){
e.preventDefault();
$(this).toggleClass('menu-btn--open');
$('.menu-nav').toggleClass('menu-nav--open');
});
body, html {
margin: 0;
}

.section {
width: 100%;
height: 100vh;
background: linear-gradient(#55efc4, #ffeaa7);
position: relative;
}

.menu-btn__wrapper {
position: absolute;
top: 20px;
right: 20px;
}

.menu-btn {
width: 50px;
height: 50px;
display: block;
background-color: #fff;
border-radius: 50%;
position: relative;
}

.menu-block {
display: flex;
flex-direction: column-reverse;
}

.menu-nav {
display: flex;
flex-direction: column;
align-items: center;
transition: .5s;
transform-origin: top center;
transform: scaleY(0) translateY(-20px);
opacity: 0;
}
.menu-nav.with-border {
background-color: #fff;
padding-bottom: 20px;
padding-top: 30px;
margin-top: -40px;
border-radius: 50px;
}
.menu-nav--open {
transform: scaleY(1) translateY(0);
opacity: 1;
}
.menu-nav__name {
position: absolute;
right: 45px;
top: 0;
margin: 0;
opacity: 0;
transition: .3s;
}
.menu-nav__name img {
idth: 30px;
height: 30px;
border-radius: 50%;
}
.menu-nav__link {
display: inline-block;
color: #000;
margin-top: 5px;
transition: .4s;
position: relative;
}

.menu-nav__img {
width: 30px;
height: 30px;
display: flex;
justify-content: center;
margin-left: auto;
margin-right: auto;
padding-top: 20%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="section">
<div class="menu-btn__wrapper">
<div class="menu-block">
<nav class="menu-nav with-border"><a class="menu-nav__link" href="#">
<p class="menu-nav__name"></p></a><a class="menu-nav__link" href="#">
<p class="menu-nav__name"></p><img class="menu-nav__img menu-img" src="https://cdn2.iconfinder.com/data/icons/world-flags-1-1/100/Britain-512.png"></a><a class="menu-nav__link" href="#">
<p class="menu-nav__name"></p><img class="menu-nav__img menu-img" src="http://icons.veryicon.com/png/Flag/Rounded%20World%20Flags/Russia%20Flag.png"></a><a class="menu-nav__link" href="#">
<p class="menu-nav__name"></p><img class="menu-nav__img menu-img" src="https://cdn.countryflags.com/thumbs/germany/flag-round-250.png"> </a></nav><a class="menu-btn" href="#"><img class="menu-nav__img" id="main-btn-img" src="https://cdn2.iconfinder.com/data/icons/world-flags-1-1/100/Britain-512.png"><span class="menu-btn__line"></span></a>
</div>
</div>
</div>

最佳答案

您需要在每个菜单项上添加onclick事件,并将主图像的src替换为单击的图像。

$(document).on('click', function (e) {
if ($(e.target).closest(".menu-btn").length === 0) {
$('.menu-nav').removeClass('menu-nav--open');
}
});

$('.menu-btn').on('click', function(e){
e.preventDefault();
$(this).toggleClass('menu-btn--open');
$('.menu-nav').toggleClass('menu-nav--open');
});

$('.menu-nav__link').on('click', function(e){
$("#main-btn-img").attr('src', $(this).find('img').attr('src'));
});
body, html {
margin: 0;
}

.section {
width: 100%;
height: 100vh;
background: linear-gradient(#55efc4, #ffeaa7);
position: relative;
}

.menu-btn__wrapper {
position: absolute;
top: 20px;
right: 20px;
}

.menu-btn {
width: 50px;
height: 50px;
display: block;
background-color: #fff;
border-radius: 50%;
position: relative;
}

.menu-block {
display: flex;
flex-direction: column-reverse;
}

.menu-nav {
display: flex;
flex-direction: column;
align-items: center;
transition: .5s;
transform-origin: top center;
transform: scaleY(0) translateY(-20px);
opacity: 0;
}
.menu-nav.with-border {
background-color: #fff;
padding-bottom: 20px;
padding-top: 30px;
margin-top: -40px;
border-radius: 50px;
}
.menu-nav--open {
transform: scaleY(1) translateY(0);
opacity: 1;
}
.menu-nav__name {
position: absolute;
right: 45px;
top: 0;
margin: 0;
opacity: 0;
transition: .3s;
}
.menu-nav__name img {
idth: 30px;
height: 30px;
border-radius: 50%;
}
.menu-nav__link {
display: inline-block;
color: #000;
margin-top: 5px;
transition: .4s;
position: relative;
}

.menu-nav__img {
width: 30px;
height: 30px;
display: flex;
justify-content: center;
margin-left: auto;
margin-right: auto;
padding-top: 20%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="section">
<div class="menu-btn__wrapper">
<div class="menu-block">
<nav class="menu-nav with-border"><a class="menu-nav__link" href="#">
<p class="menu-nav__name"></p></a><a class="menu-nav__link" href="#">
<p class="menu-nav__name"></p><img class="menu-nav__img menu-img" src="https://cdn2.iconfinder.com/data/icons/world-flags-1-1/100/Britain-512.png"></a><a class="menu-nav__link" href="#">
<p class="menu-nav__name"></p><img class="menu-nav__img menu-img" src="http://icons.veryicon.com/png/Flag/Rounded%20World%20Flags/Russia%20Flag.png"></a><a class="menu-nav__link" href="#">
<p class="menu-nav__name"></p><img class="menu-nav__img menu-img" src="https://cdn.countryflags.com/thumbs/germany/flag-round-250.png"> </a></nav><a class="menu-btn" href="#"><img class="menu-nav__img" id="main-btn-img" src="https://cdn2.iconfinder.com/data/icons/world-flags-1-1/100/Britain-512.png"><span class="menu-btn__line"></span></a>
</div>
</div>
</div>

关于javascript - 在语言面板中切换图像(标志),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51137888/

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