javascript - setInterval() ,JS实现toggle,逻辑冲突
阿神
阿神 2017-04-11 10:47:30
0
4
327
阿神
阿神

闭关修行中......

모든 응답 (4)
迷茫
    toggle  
   

    小葫芦

    那就不要直接判断相等

    y1.offsetHeight>yh
      洪涛
      window.onload = function(){ var x,y,speed,ar1,y1,s,yh,timer; x = document.getElementById('bt'); y = document.getElementById('co'); s = function(ar1,y1,yh){ clearInterval(timer); timer = setInterval(function(){ speed = ar1*(yh-y1.offsetHeight)/5;(这里做下处理,当速度小于某值的时候等于某值) speed = speed?Math.ceil(speed):Math.floor(speed); if (y1.offsetHeight===yh){ console.log(y1.offsetHeight) clearInterval(timer); }else{ y1.style.height = y1.offsetHeight + speed + "px"; } },20) } x.onclick = function(){ if(y.offsetHeight < 5){ s(1,y,400); }else{ s(1,y,0) //改个传参就行了 } } }
        黄舟
        window.onload = function() { var x, y, speed, ar1, y1, s, yh, timer; x = document.getElementById('bt'); y = document.getElementById('co'); s = function(ar1, y1, yh) { clearInterval(timer); timer = setInterval(function() { speed = ar1 * (yh - y1.offsetHeight) / 5; console.log(speed); speed = speed ? Math.ceil(speed) : Math.floor(speed); console.log(speed); speed == -0 ? speed = -(y1.offsetHeight) : ''; //当speed减少为-0的时候,直接变成p高度使p减少为0。否则无法取消定时器。 if (y1.offsetHeight === yh) { clearInterval(timer); } else { y1.style.height = y1.offsetHeight + speed + "px"; } }, 20) } x.onclick = function() { if (y.offsetHeight < 5) { s(1, y, 500); } else { s(1, y, 0) } } }
          최신 다운로드
          더>
          웹 효과
          웹사이트 소스 코드
          웹사이트 자료
          프론트엔드 템플릿
          회사 소개 부인 성명 Sitemap
          PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!