web前端js基础------制作滚动图片

1,要求

通过定时器使其出现滚动的效果

可以通过按键控制图片滚动的方向(设置两个按钮绑定点击事件)

当鼠标悬停时图片停止,鼠标离开时图片继续向前滚动(可以设置鼠标的悬停和离开事件)

参考如下

html 复制代码
			content.onmouseenter = function(){//设置鼠标悬停事件
				key = "stop";
			}
			content.onmouseleave = function(){//设置鼠标离开事件
				key = "run";
			}

图片可以持续滚动,不会出现空白(添加滚动图片)

2,参考代码(左右滚动)

html 复制代码
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<style type="text/css">
			#slide{
				width:800px;
				height: 100px;
				margin: 0 auto;
				margin-top: 100px;
				border: 1px solid black;
				overflow: hidden;
			}
			#content{
				width: 999999px;
			}
			img{
				width: 160px;
				height: 100px;
				float: left;
				margin-right: 10px;
				cursor: pointer;
			}
			#button{
				width: 100px;
				margin: 20px auto;
			}
		</style>
	</head>
	<body>
		<div id="slide">
			<div id="content">
				
				<img src="../img/1.png"/>
				<img src="../img/2.png"/>
				<img src="../img/3.png"/>
				<img src="../img/4.png"/>
				<img src="../img/5.png"/>
				<img src="../img/6.png"/>
				<img src="../img/7.png"/>
				<img src="../img/8.png"/>
				<img src="../img/9.png"/>
			</div>
		</div>
		<div id="button">
				<button type="button" id="left">向左</button>
				<button type="button" id="right">向右</button>
			</div>
		<script type="text/javascript">
			var content = document.getElementById("content");
			var left = document.getElementById("left");
			var right = document.getElementById("right");
			content.style.marginLeft = 0+"px";
			content.innerHTML = content.innerHTML+content.innerHTML+content.innerHTML;
			var key = "run";
			var dkey = "left";
			left.onclick = function(){
				dkey = "left";
			}
			right.onclick = function(){
				dkey = "right";
			}
			content.onmouseenter = function(){
				key = "stop";
			}
			content.onmouseleave = function(){
				key = "run";
			}
			dd();
			function dd(){
				var num = parseFloat(content.style.marginLeft);
				if(key == "run"){
					if(dkey == "left"){
						num-=0.5;
						if(num<=-170*9){
							num=0;
						}
					}else{
						num+=0.5;
						if(num>=0){
							num=-170*9;
						}
					}
				}
				content.style.marginLeft = num+"px";
				setTimeout("dd()",13);
			}
		</script>
	</body>
</html>

3,结果参考示例

4,参考代码(上下滚动)

html 复制代码
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			/* 设置盒子 */
			#div{
				height: 800px;
				width: 100px;
				border: 1px solid black;
				margin: 0px auto;
				overflow: hidden;			
			}
			/* 设置内容即图片的容器*/
			#content{
				height: 999999px;
			}
			/* 设置图片的样式 */
			img{
				width: 100px;
				height: 160px;
				margin-top: 10px;
				cursor: pointer;
			}
			/* 设置按钮样式*/
			#button{ 
				width: 100px;
				margin: 20px auto;
			}
		</style>
	</head>
	<body>
		<div id="div">
			<div id="content">
				<img src="../img/2ee18-231100-163232346010b0.jpg"/>
				<img src="../img/1.png"/>
				<img src="../img/2.png"/>
				<img src="../img/3.png"/>
				<img src="../img/4.png"/>
				<img src="../img/5.png"/>
				<img src="../img/6.png"/>
				<img src="../img/7.png"/>
				<img src="../img/8.png"/>
				<img src="../img/9.png"/>
			</div>
		</div>
		<div id="button">
				<button type="button" id="top">向上</button>
				<button type="button" id="buttom">向下</button>
		</div>
		<script>
			var content = document.getElementById("content");//获取内容的id
			var topSlide =document.getElementById("top");//获取向上按钮的id
			var downSlide =document.getElementById("buttom");//获取向下按钮的id
			content.innerHTML = content.innerHTML+content.innerHTML+content.innerHTML;//使内容乘于3倍
			content.style.marginTop = 0 + "px";//设置外边距初始值
			var key = "run";//设置初始是运行的
			var dkey = "top";//设置初始是向右边的
			topSlide.onclick = function(){
				dkey = "top";
			}
			downSlide.onclick = function(){
				dkey = "buttom";
			}
			content.onmouseenter = function(){//设置鼠标悬停事件
				key = "stop";
			}
			content.onmouseleave = function(){//设置鼠标离开事件
				key = "run";
			}
			
			slide();
			function slide(){
				var num = parseFloat(content.style.marginTop);
			if(key == "run"){
				if(dkey == "top"){
					num-=0.5;
					if(num<=-170*9){
						num=0;
					}
				}else{
					num+=0.5;
					if(num>=0){
						num=-170*9;
					}
				}
			}		
				content.style.marginTop = num+"px";
				setTimeout("slide()",13);//设置定时器使其运行
			}
		</script>
	</body>
</html>

效果同3

相关推荐
Можно2 分钟前
Vue 组件样式隔离完全指南:从原理到实战
前端·javascript·vue.js
bu_shuo7 分钟前
c++中对数组求和
开发语言·c++
赫瑞7 分钟前
Java中的大数处理 —— BigInteger
java·开发语言
r_oo_ki_e_7 分钟前
java25--Collection集合
java·开发语言
bearpping8 分钟前
WebSpoon9.0(KETTLE的WEB版本)编译 + tomcatdocker部署 + 远程调试教程
前端
elseif12318 分钟前
【Markdown】指南(上)
linux·开发语言·前端·javascript·c++·笔记
钛态29 分钟前
Flutter for OpenHarmony:shelf_web_socket 快速构建 WebSocket 服务端,实现端到端实时通信(WebSocket 服务器) 深度解析与鸿蒙适配指南
服务器·前端·websocket·flutter·华为·性能优化·harmonyos
初九之潜龙勿用31 分钟前
C# 解决“因为算法不同,客户端和服务器无法通信”的问题
服务器·开发语言·网络协议·网络安全·c#
紫_龙31 分钟前
最新版vue3+TypeScript开发入门到实战教程之组件通信之二
前端·javascript·typescript
不知名。。。。。。。。35 分钟前
Qt常用控件
开发语言·qt