双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

轮播图js 怎么使最后的鼠标移动事件和点击事件生效呢

saunter through life saunter through life 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}

ul,
li {
list-style: none;
}

.box {
width: 750px;
height: 600px;
border: 1px solid black;
margin: 50px auto;
position: relative;
}

#box1 {
width: 750px;
height: 600px;
position: absolute;
/* 溢出隐藏 */
overflow: hidden;
}

#box1 #list {
width: 500%;
position: absolute;
top: 0;
left: 0;
}

#box1 #list li {
width: 750px;
height: 600px;
float: left;
}

#circle_list {
width: 300px;
height: 20px;
border-radius: 10%;
/* 透明 */
background-color: rgb(0, 0, 0, 0.3);
position: absolute;
margin-top: 550px;
margin-left: 240px;
}

.circle {
float: left;
width: 16px;
height: 16px;
border-radius: 50%;
background-color: white;
margin-top: 2px;
margin-left: 35px;
/* position: absolute; */
cursor: pointer;
}

.button {
width: 750px;
height: 350px;
position: absolute;
top: 50%;
margin-top: -35px;
z-index: 3;
}

.btn {
width: 50px;
height: 70px;
background: rgba(0, 0, 0, 0.5);
color: white;
font-size: 36px;
text-align: center;
line-height: 70px;
}

.fr {
float: right;
}

.fl {
float: left;
}

.active {
background-color: red;
}
</style>
</head>

<body>
<div class="box">
<div id="box1">
<ul id="list">
<li><img src="images/pic6.jpg" alt="" width="900px" height="600px"></li>
<li><img src="images/pic2.jpg" alt="" width="900px" height="600px"></li>
<li><img src="images/pic3.jpg" alt="" width="900px" height="600px"></li>
<li><img src="images/pic4.jpg" alt="" width="900px" height="600px"></li>
<li><img src="images/pic5.jpg" alt="" width="900px" height="600px"></li>
</ul>
</div>
<div id="circle_list">
<span class="circle active"></span>
<span class="circle"></span>
<span class="circle"></span>
<span class="circle"></span>
<span class="circle"></span>
</div>
<div class="button">
<div class="btn fl" id="left"><</div>
<div class="btn fr" id="right">></div>
</div>
</div>

<script>
var list = document.getElementById('list');
var circles = document.getElementsByClassName('circle');
//自执行函数 鼠标移动 小圆点事件
// for (var i = 0; i < circles.length; i++) {
// (function(i) {
// circles[i].onmouseover = function() {
// for (var j = 0; j < circles.length; j++) {
// circles[j].className = "circle";
// }
// this.className = "circle active";
// var target = i * -750;
// moveAnimate1(list, target); //是个函数 不加等号
// }
// })(i);
// }
for (var i = 0; i < circles.length; i++) {
(function(i) {
// circles[i].onmouseover = function() {//鼠标移动事件
circles[i].onclick = function() { //点击事件
// 排他
for (var j = 0; j < circles.length; j++) {
circles[j].className = "circle";
}
// 使当前触发的小圆点变色
this.className = "circle active";
// 设置移动距离
var target = i * -750;
moveAnimate1(list, target);
}
})(i);
};
// 封装自动播放的方法 设置变量
function autoplay() {
// 作为轮播到当前的图片的位置
var flag = 1;
setInterval(function() {
// 回归初识
flag = flag > circles.length - 1 ? 0 : flag;
// 排他
for (var j = 0; j < circles.length; j++) {
circles[j].className = "circle";
}
// 使当前触发的小圆点变色
circles[flag].className = "circle active";
// 设置移动距离
var target = flag * -750;
moveAnimate1(list, target);
flag++;
}, 2000)
}
autoplay();

function moveAnimate1(ele, target) {
clearInterval(ele.interId);
ele.interId = setInterval(function() {
if (ele.offsetLeft == target) {
clearIntervar(ele.interId);
} else {
var step = (target - ele.offsetLeft) / 10;
step = step > 0 ? Math.ceil(step) : Math.floor(step);
ele.style.left = ele.offsetLeft + step + 'px';
}
}, 10);
}
//目标最大值
function moveAnimate2(ele, target) {
clearInterval(ele.interId);
ele.interId = setInterval(function() {
if (ele.offsetLeft == target) {
clearInterval(ele.interId);
} else if (ele.setInterval < target) {
var step = (ele.offsetLeft + 13) > target ? target : ele.offsetLeft + 13;
//向左走
ele.style.left = step + 'px';
} else if (ele.setInterval > target) {
var step = (ele.offsetLeft - 13) > target ? target : ele.offsetLeft - 13;
//向右走
ele.style.left = step + 'px';
}
}, 50);
}
mouse.onmouseover = function() {
//鼠标移上去的事件(停止自动轮播)
clearInterval(moveEle);
}
mouse.onmouseover = function() { //鼠标移开事件(恢复自动轮播)
move = setInterval(function() {
autoplay("left")
}, 1000);
}
left.onclick = function() { //左边按钮事件;
autoplay("left");
}
right.onclick = function() { //右边按钮事件;
autoplay("right");
}
</script>
</body>

</html>
...全文

相关推荐

javascript实现点击按钮切换轮播图功能

主要为大家详细介绍了javascript实现点击按钮切换轮播图功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

weixin_38738528

js实现轮播图效果 纯js实现图片自动切换

本文实例为大家分享了纯js实现图片自动切换的具体代码,供大家参考,具体内容如下 1.鼠标经过的时候左右两个小按钮会自动弹出,自动播放停止,点击左右小按钮可以切换图片; 2. 鼠标离开,恢复自动播放; 3. 点击下方中间几个小圆圈,也会自动切换图片; 源代码: &lt;!DOCTYPE html> &lt;html lang="en"&gt; &lt;head&gt; &lt;meta charset="UTF-8"&gt; &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt; &lt;title&gt;Document&lt;/title&gt; &lt;sty

weixin_38515362

原生js实现无缝轮播图

主要为大家详细介绍了原生js实现无缝轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

weixin_38645669

Vue.js轮播图走马灯代码实例(全)

主要介绍了Vue.js轮播图走马灯,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

weixin_38734269

JS轮播图实现简单代码

主要为大家详细介绍了JS轮播图实现简单代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

weixin_38650066
评论
收藏 已收藏
分享
返回顶部
saunter through life
JavaScript 87,983 成员 224,680 内容