zoukankan      html  css  js  c++  java
  • 使用JS完成首页轮播效果

    setInterval和setTimeout

    setTimeout()方法只运行一次,也就是说当达到设定的时间后就出发运行指定的代码,运行完后就结束了,如果还想再次执行同样的函数,可以在函数体内再次调用setTimeout(),可以达到循环调用的效果。

    setInterval()是循环执行的,即每达到指定的时间间隔就执行相应的函数或者表达式,是真正的定时器。

    <scrpt>
        function init(){
            //书写轮播图片显示的定时操作
            setInterval("changeImg()",1000);;
        }
    
        //书写函数
        var i=0;
        function changeImg(){
            i++;
            //获取图片位置并设置src属性值
            document.getElementById("img1").src="../img/"+i+".jpg";
            if(i==3){
                i=0;
            }
        }
    </script>

    源代码: 

    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="UTF-8">
    		<title>首页</title>
    		<style>
    			#father{
    				border: 0px solid red;
    				 1300px;
    				height: 2170px;
    				margin: auto;
    			}
    			/*#logo{
    				border: 0px solid black;
    				 1300px;
    				height: 50px;
    			}*/
    			.top{
    				border: 0px solid blue;
    				 431px;
    				height: 50px;
    				float: left;
    			}
    			#top{
    				padding-top: 12px;
    				height: 38px;
    			}
    			#menu{
    				border: 0px solid red;
    				 1300px;
    				height: 50px;
    				background-color: black;
    				margin-bottom: 10px;
    			}
    			ul li{
    				display: inline;
    				color: white;
    			}
    			#clear{
    				clear: both;
    			}
    			
    			#product{
    				border: 0px solid red;
    				 1300px;
    				height: 558px;
    			}
    			#product_top{
    				border: 0px solid blue;
    				 100%;
    				height: 45px;
    				padding-top: 8px;
    			}
    			#product_bottom{
    				border: 0px solid green;
    				 100%;
    				height: 500px;
    			}
    			#product_bottom_left{
    				border: 0px solid red;
    				 200px;
    				height: 500px;
    				float: left;
    			}
    			#product_bottom_right{
    				border: 0px solid blue;
    				 1094px;
    				height: 500px;
    				float: left;
    			}
    			#big{
    				border: 0px solid red;
    				 544px;
    				height: 248px;
    				float: left;
    			}
    			.small{
    				border: 0px solid blue;
    				 180px;
    				height: 248px;
    				float: left;
    				/*让里面的内容居中*/
    				text-align: center;
    			}
    			
    			#bottom{
    				text-align: center;
    			}
    			
    			a{
    				text-decoration: none;
    			}
    		</style>
    		<script>
    			function init(){
    				//书写轮图片显示的定时操作
    				setInterval("changeImg()",1000);
    			}
    			
    			//书写函数
    			var i=0
    			function changeImg(){
    				i++;
    				//获取图片位置并设置src属性值
    				document.getElementById("img1").src="../img/"+i+".jpg";
    				if(i==3){
    					i=0;
    				}
    			}
    		</script>
    	</head>
    	<body onload="init()">
    		<div id="father">
    			<!--定时弹出广告图片位置-->
    			<img src="../img/f001a62f-a49d-4a4d-b56f-2b6908a0002c_g.jpg" width="100%" style="display: none"/>
    			
    			<!--1.logo部分-->
    			<div id="logo">
    				<div class="top">
    					<img src="../img/logo2.png" height="46px"/>
    				</div>
    				<div class="top">
    					<img src="../img/header.png" height="46px" />
    				</div>
    				<div class="top" id="top">
    					<a href="#">登录</a>
    					<a href="#">注册</a>
    					<a href="#">购物车</a>
    				</div>
    			</div>
    			<div id="clear">
    				
    			</div>
    			<!--2.导航栏部分-->
    			<div id="menu">
    				<ul>
    					<a href="#"><li style="font-size: 20px;">首页</li></a>&nbsp;&nbsp;&nbsp;&nbsp;
    					<a href="#"><li>手机数码</li></a>&nbsp;&nbsp;&nbsp;&nbsp;
    					<a href="#"><li>家用电器</li></a>&nbsp;&nbsp;&nbsp;&nbsp;
    					<a href="#"><li>鞋靴箱包</li></a>&nbsp;&nbsp;&nbsp;&nbsp;
    					<a href="#"><li>孕婴保健</li></a>&nbsp;&nbsp;&nbsp;&nbsp;
    					<a href="#"><li>奢侈品</li></a>
    				</ul>
    			</div>
    			<!--3.轮播图部分-->
    			<div id="">
    				<img src="../img/1.jpg" width="100%" id="img1"/>
    			</div>
    			<!--4.最新商品-->
    			<div id="product">
    				<div id="product_top">
    					&nbsp;&nbsp;&nbsp;
    					<span style="font-size: 25px;padding-top: 8px;">最新商品</span>&nbsp;&nbsp;&nbsp;
    					<img src="../img/title2.jpg" />
    				</div>
    				<div id="product_bottom">
    					<div id="product_bottom_left">
    						<img src="../img/big01.jpg" width="100%" height="100%"/>
    					</div>
    					<div id="product_bottom_right">
    						<div id="big">
    							<a href="#"><img src="../img/middle01.jpg" width="100%" height="100%"/></a>
    						</div>
    						<div class="small">
    							<img src="../img/small03.jpg" />
    							<a href="#"><p style="color: gray;">电炖锅</p></a>
    							<p style="color: red;">¥399</p>
    						</div>
    						<div class="small">
    							<img src="../img/small03.jpg" />
    							<a href="#"><p style="color: gray;">电炖锅</p></a>
    							<p style="color: red;">¥399</p>
    						</div>
    						<div class="small">
    							<img src="../img/small03.jpg" />
    							<a href="#"><p style="color: gray;">电炖锅</p></a>
    							<p style="color: red;">¥399</p>
    						</div>
    						<div class="small">
    							<img src="../img/small03.jpg" />
    							<a href="#"><p style="color: gray;">电炖锅</p></a>
    							<p style="color: red;">¥399</p>
    						</div>
    						<div class="small">
    							<img src="../img/small03.jpg" />
    							<a href="#"><p style="color: gray;">电炖锅</p></a>
    							<p style="color: red;">¥399</p>
    						</div>
    						<div class="small">
    							<img src="../img/small03.jpg" />
    							<a href="#"><p style="color: gray;">电炖锅</p></a>
    							<p style="color: red;">¥399</p>
    						</div>
    						<div class="small">
    							<img src="../img/small03.jpg" />
    							<a href="#"><p style="color: gray;">电炖锅</p></a>
    							<p style="color: red;">¥399</p>
    						</div>
    						<div class="small">
    							<img src="../img/small03.jpg" />
    							<a href="#"><p style="color: gray;">电炖锅</p></a>
    							<p style="color: red;">¥399</p>
    						</div>
    						<div class="small">
    							<img src="../img/small03.jpg" />
    							<a href="#"><p style="color: gray;">电炖锅</p></a>
    							<p style="color: red;">¥399</p>
    						</div>
    					</div>
    				</div>
    			</div>
    			<!--5.广告图片-->
    			<div id="">
    				<img src="../img/ad.jpg" width="100%"  />
    			</div>
    			<!--6.热门商品-->
    			<div id="product">
    				<div id="product_top">
    					&nbsp;&nbsp;&nbsp;
    					<span style="font-size: 25px;padding-top: 8px;">热门商品</span>&nbsp;&nbsp;&nbsp;
    					<img src="../img/title2.jpg" />
    				</div>
    				<div id="product_bottom">
    					<div id="product_bottom_left">
    						<img src="../img/big01.jpg" width="100%" height="100%"/>
    					</div>
    					<div id="product_bottom_right">
    						<div id="big">
    							<a href="#"><img src="../img/middle01.jpg" width="100%" height="100%"/></a>
    						</div>
    						<div class="small">
    							<img src="../img/small03.jpg" />
    							<a href="#"><p style="color: gray;">电炖锅</p></a>
    							<p style="color: red;">¥399</p>
    						</div>
    						<div class="small">
    							<img src="../img/small03.jpg" />
    							<a href="#"><p style="color: gray;">电炖锅</p></a>
    							<p style="color: red;">¥399</p>
    						</div>
    						<div class="small">
    							<img src="../img/small03.jpg" />
    							<a href="#"><p style="color: gray;">电炖锅</p></a>
    							<p style="color: red;">¥399</p>
    						</div>
    						<div class="small">
    							<img src="../img/small03.jpg" />
    							<a href="#"><p style="color: gray;">电炖锅</p></a>
    							<p style="color: red;">¥399</p>
    						</div>
    						<div class="small">
    							<img src="../img/small03.jpg" />
    							<a href="#"><p style="color: gray;">电炖锅</p></a>
    							<p style="color: red;">¥399</p>
    						</div>
    						<div class="small">
    							<img src="../img/small03.jpg" />
    							<a href="#"><p style="color: gray;">电炖锅</p></a>
    							<p style="color: red;">¥399</p>
    						</div>
    						<div class="small">
    							<img src="../img/small03.jpg" />
    							<a href="#"><p style="color: gray;">电炖锅</p></a>
    							<p style="color: red;">¥399</p>
    						</div>
    						<div class="small">
    							<img src="../img/small03.jpg" />
    							<a href="#"><p style="color: gray;">电炖锅</p></a>
    							<p style="color: red;">¥399</p>
    						</div>
    						<div class="small">
    							<img src="../img/small03.jpg" />
    							<a href="#"><p style="color: gray;">电炖锅</p></a>
    							<p style="color: red;">¥399</p>
    						</div>
    					</div>
    				</div>
    			</div>
    			<!--7.广告图片-->
    			<div id="">
    				<img src="../img/footer.jpg" width="100%"/>
    			</div>
    			<!--8.友情链接和版权信息-->
    			<div id="bottom">
    				<a href="#">关于我们</a>
    				<a href="#">联系我们</a>
    				<a href="#">招贤纳士</a>
    				<a href="#">法律声明</a>
    				<a href="#">友情链接</a>
    				<a href="#">支付方式</a>
    				<a href="#">配送方式</a>
    				<a href="#">服务声明</a>
    				<a href="#">广告声明</a>
    				<p>
    					Copyright © 2005-2016 传智商城 版权所有 
    				</p>
    			</div>
    		</div>
    	</body>
    </html>
    
  • 相关阅读:
    网络处理1-异步GET请求
    &lt;十二&gt;读&lt;&lt;大话设计模式&gt;&gt;之状态模式
    oracle的shared、dedicated模式解析
    java 调用ant的自己定义task,默认不是build.xml 的一点问题
    【Android开发-8】生命周期,Activity中打开另外一个Activity
    Robot Framework自己主动化測试框架之我见
    三张图教你生成一个Android jar 库。
    Array types are now written with the brackets around the element type问题的解决方法
    HDU 4085 Peach Blossom Spring 斯坦纳树 状态压缩DP+SPFA
    Java 线程池ThreadPoolExecutor简单应用
  • 原文地址:https://www.cnblogs.com/strawqqhat/p/10602240.html
Copyright © 2011-2022 走看看