本篇文章为大家展示了利用jquery怎么实现一个淡入淡出的轮播图效果,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
实现方法如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
* {
padding: 0px;
margin: 0px;
}
/* 盒子 */
.box {
position: relative;
}
/* banner图 */
.box,
.banner {
width: 1000px;
height: 500px;
margin: 50px auto;
}
.box .banner {
position: relative;
list-style: none;
}
.banner li {
display: none;
}
.box ul li img {
position: absolute;
}
.banner .b1 {
display: block;
}
span {
background: rgb(0, 0, 0, 0.5);
width: 40px;
height: 50px;
display: block;
position: absolute;
left: 0px;
top: 50%;
z-index: 3;
line-height: 50px;
text-align: center;
cursor: pointer;
font-family: "宋体";
font-size: 20px;
color: white;
}
.right {
left: auto;
right: 0px;
}
.bottom {
position: absolute;
bottom: 10px;
left: 50%;
transform: translateX(-50%);
list-style: none;
}
.bottom li {
width: 10px;
height: 10px;
background: rgb(255, 255, 255);
float: left;
border-radius: 50%;
margin: 0px 5px;
/* 重影 */
box-shadow: 0 0 5px rgba(0, 0, 0, 0.5);
cursor: pointer;
}
.bottom .on {
background: rgb(255, 153, 0);
}
</style>
<script src="https://cache.yisu.com/upload/information/20201213/112/49679.html" alt="">
</li>
<li>
<img src="../img/timg1.jpg" alt="">
</li>
<li>
<img src="../img/timg2.jpg" alt="">
</li>
<li>
<img src="../img/timg3.jpg" alt="">
</li>
<li>
<img src="../img/timg4.jpg" alt="">
</li>
</ul>
<!-- 左右切换 -->
<span class="left">
< </span> <span class="right">>
</span>
<!-- 底部按钮 -->
<ol class="bottom">
<li class="on"></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ol>
</div>
</body>
<script>
var $box = $(".box");
var $bli = $(".box ul li");
var $bleft = $(".box .left");
var $bright = $(".box .right");
var $dot = $(".box .bottom li");
var timer = null;
var num = 0;
// 因为几行代码一样 所以进行代码封装
function play() {
num = num % $bli.length;
$bli.eq(num).stop().fadeIn(800).siblings().fadeOut(800);
$dot.eq(num).addClass("on").siblings().removeClass("on");
}
function autoplay() {
timer = setInterval(function () {
num++;
play();
}, 2000);
};
autoplay();
// 当鼠标放上去的时候 计时器停止 移除的时候计时器工作
// $box.hover(function() {
// clearInterval(timer);
// },function() {
// autoplay();
// });
// 下面是同样的效果 另一种写法
$box.mouseover(function () {
clearInterval(timer);
});
$box.mouseout(function () {
autoplay();
});
$bleft.click(function () {
num--;
play();
});
$bright.click(function () {
num++;
play();
});
$dot.click(function (event) {
var index = $(this).index();
num = index;
$bli.eq(num).stop().fadeIn(800).siblings().fadeOut(800);
$dot.eq(num).addClass("on").siblings().removeClass();
});
</script>
</html>
其实淡入淡出轮播图的核心思想就是获取到当前的索引,根据索引找出图片的索引然后更改索引达到一个图片显示 其他图片消失 然后轮流交替进行的。最主要的核心思想在上面sibinling()中 通过这个方法让当前元素进行淡入操作但是让其他元素进行淡出操作,所以效果上就形成了,一张张图盘轮流出来的效果。
上述内容就是利用jquery怎么实现一个淡入淡出的轮播图效果,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。