• 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>
    
  • 相关阅读:
    Hyper-V安装Centos7
    【DDD】使用领域驱动设计思想实现业务系统
    关于数据库‘状态’字段设计的思考与实践
    如何快速处理线上故障
    《企业应用架构模式》读后感
    java使用何种类型表示精确的小数?
    【项目经验】数据迁移总结
    springMVC引入Validation详解
    【DDD】领域驱动设计实践 —— 一些问题及想法
    【系统设计】“查询推荐好友”服务在不同架构风格下如何设计?
  • 原文地址:https://www.cnblogs.com/haicheng/p/3718565.html
Copyright © 2020-2023  润新知