鼠标移到左右两边是正常速度 但是移到上面按钮就会出现一次型做2次减法

鼠标移到左右两边是正常速度 但是移到上面按钮就会出现一次型做2次减法

<!DOCTYPE html>

<html>

<head>

<title></title>

<link rel="stylesheet" type="text/css" href="sy_css.css">

<script src="sy_js.js" type="text/javascript" charset="utf-8" async defer></script>

</head>

<body>

<div class="header" id="main">

<div class="head">

<div class="head-img">

     <a href="#" title=""><img src="img/loge.png"></a>

    </div>

    <div class="head-table">

    <ul>

    <li><a href="#">首页</a></li>

    <li><a href="#">创作中心</a></li>

    <li><a href="#">个人中心</a></li>

    </ul>

    <div class="form">

    <form>

    <input type="text" name="" placeholder="搜CSDN" onfocus="show1()"  onblur="show2()">

    <a href="#">

    <img src="img/search.png" alt="" id="img1">

    <img src="img/search (1).png" alt="" id="img2"></a>

    </form>

    </div>

    <div class="on">

      <a href="#" title="">登录/注册</a>  

    </div>

    </div>

</div>

</div>

<div class="img-main" id="img-main">

<!--轮播图片-->

<div class="banner" id="banner">

  <a href="" title="">

  <div class="banner-slide slide1 active">

 

  </div>

  </a> 

  <a href="" title="">

  <div class="banner-slide slide2">

 

  </div>

  </a> 

  <a href="" title="">

  <div class="banner-slide slide3">

 

  </div>

  </a> 

</div>

<!--上一张下一张按钮-->

<a href="javescript:void(0)" title="" class="button prev"></a>

<a href="javescript:void(0)" title="" class="button next"></a>

<!--圆点导航-->

<div class="dots">

<span class="active"></span>

<span></span>

<span></span>

</div>

</div>

</body>

</html>




body{

background-color:#F0F0F0;

margin:0;

padding: 0;

}

a{

color: black;

text-decoration: none;

}


.header{

width: 100%;

height: 50px;

background-color: white;

margin: 0 auto;

overflow: hidden;


}

.head{

width: 1050px;

margin: 0 auto;

overflow: hidden;

position: relative;

}

.head-img{

height: 50px;

width: 116px;

position: relative;

float: left;

}

.head-img img{

margin: auto 0;

position: absolute;

top: 50%;

margin-top: -16px;

}


.head-table{

background-color: white;

width: 700px;

height: 50px;

float: left;

overflow: hidden;

}


.head-table ul{

display: table;

padding: 0;

margin: 0;

line-height: 50px;

float: left;

}


.head-table li{

display: table-cell;

vertical-align: middle;

}


.head-table li a{

display: block;

height: 50px;

width: 80px;

text-align: center;

color: #666666;

}


.head-table a:hover{

background-color: #F0F0F0;


}


.head-table .form{

height: 50px;

float: left;

}


.form{

position: relative;

}


.form input{

height: 28px;

width: 240px;

background-color: #F0F0F0;

font-size: 18px;

position: absolute;

top: 50%;

margin-top: -17px;

margin-left: 200px;

border-radius: 5px;

}


.form img{

height: 31px;

position: absolute;

margin-left: 411.7px;

top: 50%;

margin-top: -17px;

background-color: #C0C0C0;

border-radius: 5px;

}


.form #img2{

display: none;

background-color: #DD0000;

}


.form input{

outline: none;

border-width: 1px;

}


.on{

right: 0;

position: absolute;

font-size: 20px;

height: 50px;

line-height: 50px;

}


.on a{

color: #666666;

}


.on:hover{

background-color: #F0F0F0;

}


.img-main{

width: 1050px;

height: 400px;

margin: 5px auto;

overflow: hidden;

position: relative;

}


.banner{

width: 1050px;

height: 400px;

margin: 5px auto;

background-color: yellow;

overflow: hidden;

position: relative;

}


.banner-slide{

width: 1050px;

height: 400px;

background-repeat: no-repeat;

position: absolute;

display: none;

}


.slide1{

background-image: url(img/bg1.jpg);

}

.slide2{

background-image: url(img/bg2.jpg);

}

.slide3{

background-image: url(img/bg3.jpg);

}


.active{

display: block;

}


.button{

position: absolute;

width: 40px;

height: 80px;

top: 50%;

margin-top: -20px;

background: url(img/arrow.png) no-repeat center center; 

}


.button:hover{

background-color:#333;

opacity: 0.8;

filter:alpha(opacity=80); 

}


.prev{

transform:rotate(180deg);

}


.next{

left: auto;

right: 0;

}


.dots{

position: absolute;

right: 0;

bottom: 24px;

text-align: right;

right: 20px;

}


.dots span{

display: inline-block;

width: 12px;

height: 12px;

border-radius: 50%;

background:rgba(7,17,27,0.4);

box-shadow: 0 0 0 2px rgba(255,255,255,0.8) inset;

margin-left: 8px;

line-height: 12px;

cursor: pointer;

}


.dots span.active{

box-shadow: 0 0 0 2px rgba(7,17,27,0.4) inset;

background:#fff;

}




function byId(id){

return typeof(id) === "string"?document.getElementById(id):id;

}

function show1(){

var a = byId("img1");

a.style.display="none";

var b = byId("img2");

b.style.display = "inline";

}


function show2(){

var a = byId("img1");

a.style.display="inline";

var b = byId("img2");

b.style.display = "none";

}


var index = 0,

timer = null,

pics = byId("banner").getElementsByTagName("div"),

len = pics.length;


function slideImg(){

var main = byId("img-main");

main.onmouseover = function(){

}


main.onmouseout = function(){

timer = setInterval(function(){

index++;

if(index>=len){

index=0;

}

changeImg();

console.log(index);

},3000)

}

}


function changeImg(){

pics[index].style.display = 'block';

}


slideImg();


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

3回答
好帮手慕小尤 2020-05-12 15:01:47

同学你好,1. setInterval()是一个计时器,如下图所示:根据同学的代码会每隔三秒会调用一次方法,也就是同学看到的1--2--0,所以连续执行并不是bug。

http://img1.sycdn.imooc.com//climg/5eba491109d66ec306440508.jpg

2. 同学描述的二次减法,应该是从2直接跳转为0吧,如果是,则是正常现象,因当index大于等于3时,就会为index赋值为0,所以会出现此现象。如果不是,则建议同学重新描述一下问题。

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

提问者 源治泷谷 2020-05-12 13:18:36

我发现当鼠标刚刚移出图片超链接也就是从指头型鼠标变为正常鼠标样式时就会出现这个问题

提问者 源治泷谷 2020-05-12 13:04:59
  • 提问者 源治泷谷 #1
    鼠标向左右空白部分移动 是1--2--0;但是如果是从图片移到首页那一行就会出现12--0--12--0,有连续执行的bug
    2020-05-12 13:06:36
  • 提问者 源治泷谷 #2
    每次刷新效果还会不同 不知道哪里的问题
    2020-05-12 13:10:07
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师