我有一个语言面板,其中一个image
(main-image
)显示页面加载时的状态。我还有另外三个image's
,它们在页面加载时被隐藏。
问题是当点击其他image's
之一时如何切换主图像。我需要点击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>
答案 0 :(得分:2)
您需要在每个菜单项上添加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>