首页 > 代码库 > 无缝切换效果
无缝切换效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <link rel="stylesheet" href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css"> <style> *{margin: 0;padding: 0;} li{list-style: none;} ul{position: absolute;} li{width: 100px;height: 100px;float: left;text-align:center;line-height: 100px;box-sizing: border-box;border:1px solid red;} #box{width: 400px;height: 100px;overflow: hidden;position: relative;border:1px solid blue;margin:20px auto;} .clearfix:after{display:block;content:".";height: 0;visibility:hidden;clear:both;} div.box2{width: 400px;margin: 0 auto;} </style> </head> <body> <div id="box"> <ul id="list" class="clearfix"> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> </ul> </div> <div class="box2"> <button id="btn" class="btn btn-primary btn-block">切换</button> </div> <script src="http://www.mamicode.com/animate.js"></script> <script> var box=document.getElementById("box"); var li=document.getElementsByTagName("li"); var ul=document.getElementById("list"); var btn=document.getElementById("btn"); var iTrue=true; function getWidth(){ ul.style.width = li.length*(li[0].offsetWidth) + ‘px‘; } getWidth(); btn.onclick=function(){ if(iTrue){ iTrue=false; for(var i=0;i<4;i++){ var oli=li[i].cloneNode(true); ul.appendChild(oli); getWidth(); } } ul.animate({"left":"-400px"},function(){ for(var i=0;i<4;i++){ ul.removeChild(li[0]); ul.style.left=0; } iTrue=true; }); } </script> </body> </html>
引用的animate方法:
Element.prototype.animate=animate; Element.prototype.getStyle=getStyle; function animate(json,callback) { clearInterval(this.timer); for (var attr in json) { var that = this; this.timer = setInterval(function () { if (attr == ‘opacity‘) { that.icur = Math.round(parseFloat(that.getStyle()[attr]) * 100); } else { that.icur = parseInt(that.getStyle()[attr]); } that.speed = (parseInt(json[attr]) - that.icur) / 10; that.speed = that.speed > 0 ? Math.ceil(that.speed) : Math.floor(that.speed); if (attr == ‘opacity‘) { that.style.filter = ‘alpha(opacity:‘ + that.icur + that.speed + ‘)‘; that.style.opacity = (that.icur + that.speed) / 100; } else { that.style[attr] = that.icur + that.speed + "px"; }; if(that.icur==parseInt(json[attr])){ //flags=true; clearInterval(that.timer); if(callback){ callback(); } } }, 20); } } function getStyle() { if (this.currentStyle) { return this.currentStyle; } else { return document.defaultView.getComputedStyle(this, null); } }
无缝切换效果
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。