- <!DOCTYPE HTML>
- <html>
- <head>
- <title> New Document </title>
- <meta http-equiv="Content-Type" content="text/html;charset=GBK" />
- <base target='_blank'/>
- <style>
- body,div,ul,li{margin:0;padding:0;}
- ul,li{list-style:none;}
- img,a{display:block;}
- img{border:none;}
- .switch{overflow:hidden;width:300px;height:66px;border:1px solid #ccc;margin:50px auto 0;}
- .prev,.next{float:left;width:50px;margin-top:20px;color:red;cursor:pointer;}
- .scroll{width:198px;height:64px;overflow:hidden;float:left;border:1px solid #ccc;}
- .list{display:block;height:64px;}
- .list li{float:left;overflow:hidden;width:64px;height:64px;padding:0 1px;}
- </style>
- <script src="http://a.tbcdn.cn/??s/kissy/1.2.0/kissy-min.js"></script>
- </head>
- <body>
- <div class='switch'>
- <span class='prev'>上一页</span>
- <div class='scroll'>
- <ul class='list' id='list'>
- <li><a href="#" title=''><img src='10.jpg' width='64' height='64'/></a></li>
- <li><a href="#" title=''><img src='20.jpg' width='64' height='64'/></a></li>
- <li><a href="#" title=''><img src='30.jpg' width='64' height='64'/></a></li>
- </ul>
- </div>
- <span class='next'>下一页</span>
- </div>
- <script>
- /**
- * javascript左右控制按钮
- * @param
- */
- var switchable = (function(){
- var S = KISSY,D = S.DOM,E = S.Event;
- var max = 0,itemWidth = 0,items = D.query('#list li'),
- list = D.get('.list'),prev = D.get('.prev'),next = D.get('.next'),
- timer = null,flag = false,count=0,action = 'add';
- for(var i=0;i<items.length;i++){
- itemWidth = items[i].offsetWidth;
- max +=itemWidth;
- }
- var r = max *2; /* 给外层宽度的2倍 为动画做好准备 */
- D.css('.list','width',r);
- list.innerHTML +=list.innerHTML; /* 同理 html结构增加一倍 */
- var obj = {
- prev : function(){
- if(!flag){
- action = "minus"; //只是为了增加一个标志 和flag一样
- var that = this;
- timer = setInterval(function(){
- that.move();
- },10)
- }
- },
- next : function(){
- if(!flag){
- action = 'add';
- var that = this;
- timer = setInterval(function(){
- that.move();
- },10)
- }
- },
- move : function(){
- var list_nd = KISSY.all('.scroll');
- if(count < itemWidth){
- var n = list_nd.scrollLeft();
- if(action=='add'){
- if(n>=max){
- list_nd.scrollLeft(0);
- n = list_nd.scrollLeft();
- list_nd.scrollLeft(n+2);
- }else{
- list_nd.scrollLeft(n+2);
- }
- }else{
- if(n<=0){
- list_nd.scrollLeft(max);
- n = list_nd.scrollLeft();
- list_nd.scrollLeft(n-2);
- }else{
- list_nd.scrollLeft(n-2);
- }
- }
- count = count + 2;
- }else{
- flag = false;
- clearInterval(timer);
- count = 0;
- }
- }
- }
- E.on(prev,'click',function(){
- obj.prev();
- });
- E.on(next,'click',function(){
- obj.next();
- })
- })()
- </script>
- </body>
- </html>
转载于:https://blog.51cto.com/tugenhua/816267