zoukankan      html  css  js  c++  java
  • WEB前端:05_scroll滚动(图片/文字滚动)

    scroll滚动(图片/文字滚动)

    网站常用效果之一,以下为简化版,用于学习javascript基础知识。

    效果图:

    scroll滚动(图片/文字滚动)- 纯JS简化版

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>scroll滚动 - 纯js简化版</title>
    <style type="text/css">
    *{margin: 0; padding: 0;}
    #probox{ position: relative; border: 1px solid #ccc; 1000px;overflow:hidden;height:120px; margin: 10px auto;}
    #prolist{ position: relative; 2000px; height: 120px; list-style: none; margin: 0; padding: 0;}
    #prolist li {float: left;  250px; height: 120px; text-align: center;}
    #left,#right{ background: #333; color: #fff; padding: 5px; cursor:pointer;}
    </style>
    <script type="text/javascript">
    window.onload = function() {
    	var probox = document.getElementById('probox');
    	var prolist = document.getElementById('prolist');
    	var prolistli = prolist.getElementsByTagName('li');
    	var btnleft = document.getElementById('left');
    	var btnright = document.getElementById('right');
    
    	var cut = prolistli[1].offsetLeft - prolistli[0].offsetLeft;
    	var len = prolist.offsetLeft;
    	var timer = null;
    
    	function autoplay() {
    		if(timer) {
    			clearInterval(timer);
    		}
    		timer = setInterval(function() {
    			if(prolist.offsetLeft == -cut) {
    				clearInterval(timer);
    				prolist.appendChild(prolistli[0]);
    				prolist.style.left = "0px";
    			} else {
    				prolist.style.left = prolist.offsetLeft - 10 + "px";
    			}
    		}, 30)
    	}
    
    	probox.onmouseover = function() {
    		clearInterval(o);
    	}
    
    	probox.onmouseout = function() {
    		o = setInterval(autoplay, 3000);
    	}
    
    	btnleft.onclick = function() {
    		clearInterval(o);
    		if(timer) {
    			clearInterval(timer);
    		}
    		
    		timer = setInterval(function() {
    			if(prolist.offsetLeft == 0) {
    				clearInterval(timer);
    				prolist.insertBefore(prolistli[prolistli.length - 1], prolist.firstChild);
    				prolist.style.left = -cut + "px";
    			} else {
    				prolist.style.left = prolist.offsetLeft + 10 + "px";
    			}
    		}, 30);
    
    		o = setInterval(autoplay, 3000);
    	}
    
    	btnright.onclick = function() {
    		clearInterval(o);
    		if(timer) {
    			clearInterval(timer);
    		}
    		timer = setInterval(function() {
    			if(prolist.offsetLeft == -cut) {
    				clearInterval(timer);
    				prolist.appendChild(prolistli[0]);
    				prolist.style.left = "0px";
    			} else {
    				prolist.style.left = prolist.offsetLeft - 10 + "px";
    			}
    		}, 30);
    
    		o = setInterval(autoplay, 3000);
    	}
    
    	o = setInterval(autoplay, 3000);
    
    }
    </script>
    </head>
    <body>
    
    <div id="probox">
    	<ul id="prolist">
    		<li><img src="tab1.jpg" width="220" height="120" /></li>
    		<li><img src="tab2.jpg" width="220" height="120" /></li>
    		<li><img src="tab3.jpg" width="220" height="120" /></li>
    		<li><img src="tab4.jpg" width="220" height="120" /></li>
    		<li><img src="tab5.jpg" width="220" height="120" /></li>
    		<li><img src="tab6.jpg" width="220" height="120" /></li>
    		<li><img src="tab7.jpg" width="220" height="120" /></li>
    		<li><img src="tab8.jpg" width="220" height="120" /></li>
    	</ul>
    </div>
    <span id="left">上一个</span> <span id="right">下一个</span>
    
    </body>
    </html>
    
  • 相关阅读:
    h5之js生成二维码
    h5手势密码开发(使用jq)
    html可用于跨域的三个标签
    原生js实现ajax
    js中parentNode,parentElement,childNodes,children
    js字符串操作之substr与substring
    pyhton之os.path
    [bzoj1510][POI2006]Kra-The Disks_暴力
    [bzoj4994][Usaco2017 Feb]Why Did the Cow Cross the Road III_树状数组
    [bzoj2506]calc_分块处理
  • 原文地址:https://www.cnblogs.com/haicheng/p/3718565.html
Copyright © 2011-2022 走看看