gpt4 book ai didi

javascript - 循环浏览图像直到悬停jquery

转载 作者:行者123 更新时间:2023-11-29 19:23:10 24 4
gpt4 key购买 nike

我希望图像单独循环显示,比如每 2 秒更改一次图像,然后在悬停相关链接时切换到正确的图像。以下是我到目前为止所取得的成就。现在,我得到了要在悬停时显示的图像,但我不确定如何让它们循环。

$("#one").on({
mouseover: function () {
timer = setTimeout(function () {
$("#first").removeClass('hidden').css('opacity', '1');
}, 300);
},
mouseout: function () {
clearTimeout(timer);
$("#first").css({
'opacity': '0'
}).addClass('hidden');
}
});

https://jsfiddle.net/enwed7b9/1/

最佳答案

您的 HTML 中有一些损坏的图像链接。但这应该可以解决问题: https://jsfiddle.net/pengyanb/st47yxqb/6/

var imageIndex = 0;
var periodicTimer;
setPeriodicTimer();

function setPeriodicTimer()
{
periodicTimer = setTimeout(function(){
$('img').addClass('hidden').css('opacity', '0');
console.log('imageIndex: '+imageIndex);
switch(imageIndex)
{
case 0:
$('#first').removeClass('hidden').css('opacity', '1');
break;
case 1:
$('#second').removeClass('hidden').css('opacity', '1');
break;
case 2:
$('#third').removeClass('hidden').css('opacity', '1');
break;
case 3:
$('#fourth').removeClass('hidden').css('opacity', '1');
break;

case 4:
$('#fifth').removeClass('hidden').css('opacity', '1');
break;
case 5:
$('#sixth').removeClass('hidden').css('opacity', '1');
break;
case 6:
$('#seventh').removeClass('hidden').css('opacity', '1');
break;
}
imageIndex++;
if(imageIndex > 6)
imageIndex = 0;
setPeriodicTimer();
}, 2000);
}

$("#one").on({
mouseover: function () {
clearTimeout(periodicTimer);
imageIndex = 0;
$('img').addClass('hidden').css('opacity', '0');
timer = setTimeout(function () {
$("#first").removeClass('hidden').css('opacity', '1');
}, 300);
},
mouseout: function () {
setPeriodicTimer();
clearTimeout(timer);
$("#first").css({
'opacity': '0'
}).addClass('hidden');
}
});
$("#two").on({
mouseover: function () {
clearTimeout(periodicTimer);
imageIndex = 1;
$('img').addClass('hidden').css('opacity', '0');
timer = setTimeout(function () {
$("#second").removeClass('hidden').css('opacity', '1');
}, 300);
},
mouseout: function () {
setPeriodicTimer();
clearTimeout(timer);
$("#second").css({
'opacity': '0'
}).addClass('hidden');
}
});
$("#three").on({
mouseover: function () {
clearTimeout(periodicTimer);
imageIndex = 2;
$('img').addClass('hidden').css('opacity', '0');
timer = setTimeout(function () {
$("#third").removeClass('hidden').css('opacity', '1');
}, 300);
},
mouseout: function () {
setPeriodicTimer();
clearTimeout(timer);
$("#third").css({
'opacity': '0'
}).addClass('hidden');
}
});
$("#four").on({
mouseover: function () {
clearTimeout(periodicTimer);
imageIndex = 3;
$('img').addClass('hidden').css('opacity', '0');
timer = setTimeout(function () {
$("#fourth").removeClass('hidden').css('opacity', '1');
}, 300);
},
mouseout: function () {
setPeriodicTimer();
clearTimeout(timer);
$("#fourth").css({
'opacity': '0'
}).addClass('hidden');
}
});
$("#five").on({
mouseover: function () {
clearTimeout(periodicTimer);
imageIndex = 4;
$('img').addClass('hidden').css('opacity', '0');
timer = setTimeout(function () {
$("#fifth").removeClass('hidden').css('opacity', '1');
}, 300);
},
mouseout: function () {
setPeriodicTimer();
clearTimeout(timer);

$("#fifth").css({
'opacity': '0'
}).addClass('hidden');
}
});
$("#six").on({
mouseover: function () {
clearTimeout(periodicTimer);
imageIndex = 5;
$('img').addClass('hidden').css('opacity', '0');
timer = setTimeout(function () {
$("#sixth").removeClass('hidden').css('opacity', '1');
}, 300);
},
mouseout: function () {
setPeriodicTimer();
clearTimeout(timer);
$("#sixth").css({
'opacity': '0'
}).addClass('hidden');
}
});
$("#seven").on({
mouseover: function () {
clearTimeout(periodicTimer);
imageIndex = 6;
$('img').addClass('hidden').css('opacity', '0');
timer = setTimeout(function () {
$("#seventh").removeClass('hidden').css('opacity', '1');
}, 300);
},
mouseout: function () {
setPeriodicTimer();
clearTimeout(timer);
$("#seventh").css({
'opacity': '0'
}).addClass('hidden');
}
});
.box {
position: relative;
display: inline-block;
height: 15px;
line-height: 15px;
text-align: center;
transition: .3s;
cursor: pointer;
margin-bottom: 4px;
margin-right: 3px;
padding-bottom: 7px
}

.image {
width: 100%;
z-index: -1
}
.image img {
margin: auto;
position: absolute;
top: 0;
left: 10%;
bottom: 0;
right: 0;
width: 55%;
transition: ease-out .4s;
z-index: -1
}
.hidden {

opacity: 1
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="project-link-2">
<li class="box" id="one"> <a class="project-link" href="#modal1"><span>Modurra Shelving</span> </a>
</li>
<br>
<li class="box" id="two"> <a class="project-link" href="#modal2"><span>Kami Bicycle Helmet</span> </a>
</li>
<br>
<li class="box" id="three"> <a class="project-link" href="#modal3"><span>Revamping Language Learning</span> </a>
</li>
<br>
<li class="box" id="four"> <a class="project-link" href="#modal4"><span>Sports Innovation Challenge</span> </a>
</li>
<br>
<li class="box" id="five"> <a class="project-link" href="#modal5"><span>Lights Out</span></a>
</li>
<br>
<li class="box" id="six"> <a class="project-link" href="#modal6"><span>Maru Personal Speaker Design</span> </a>
</li>
<br>
<li class="box" id="seven"> <a class="project-link" href="#modal7"><span>A Few Casual Projects</span> </a>
</li>
</ul>



<div class="image">
<img alt="" class="hidden" id="first" src="http://consequenceofsound.files.wordpress.com/2013/06/datside-ram.jpg">
</div>
<!-- Lights Out -->
<div class="image">
<img alt="" class="hidden" id="second" src="http://tctechcrunch2011.files.wordpress.com/2012/03/random_house_logo.jpg">
</div>
<!-- Modurra -->
<div class="image">
<img alt="" class="hidden" id="third" src="work/Wordpix/splash_word.jpg">
</div>
<!-- WordPix -->
<div class="image">
<img alt="" class="hidden" id="fourth" src="work/Sports/splash_sports.jpg">
</div>
<!-- Luna -->
<div class="image">
<img alt="" class="hidden" id="fifth" src="work/Lights Out/Lights-Out-Gif.gif">
</div>
<!-- Kami -->
<div class="image">
<img alt="" class="hidden" id="sixth" src="work/Maru/splash_maru.jpg">
</div>
<!-- Misc -->
<div class="image">
<img alt="" class="hidden" id="seventh" src="work/Misc/splash_comp.jpg">
</div>
</div>

关于javascript - 循环浏览图像直到悬停jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32171334/

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