实现鼠标滚动一下页面就上下翻一页的效果

<!doctype html>
<html lang="en">

	<head>
		<meta charset="UTF-8">
		<title>Document</title>
		<link rel="stylesheet" href="http://cdn.bootcss.com/font-awesome/4.2.0/css/font-awesome.min.css">
	</head>
	<style type="text/css">
		*{
			margin: 0;
			padding: 0;
		}
		html,body{
			width: 100%;
			height: 100%;
		}
		.page{
			height: 100%;
			width: 100%;
			font-size: 126px;
			display: -webkit-box; /*Safari*/
			display: -moz-box; /*Firefox*/
			display: -ms-flexbox; /*IE*/
			display: -webkit-flex; /*Chrome*/
			display: flex;
			-webkit-box-align: center;
			-moz-box-align: center;
			-ms-flex-align: center;
			-webkit-align-items: center;
			align-items: center;
			-webkit-box-pack: center;
			-moz-box-pack: center;
			-ms-flex-pack: center;
			-webkit-justify-content: center;
			justify-content: center;
		}
		#mao{
			position: fixed;
			right: 0;
			bottom: 0;
		}
	</style>

	<body>



		<div class="page" id="item-1">Prat 1</div>
		<div class="page" id="item-2">Prat 2</div>
		<div class="page" id="item-3">Prat 3</div>
		<div class="page" id="item-4">Prat 4</div>
		<div class="page" id="item-5">Prat 5</div>
		<div class="page" id="item-6">Prat 6</div>
		<div class="page" id="item-7">Prat 7</div>

		<script type="text/javascript">
			//			var oldy = 0,tem = 0,fullheight = document.body.clientHeight;
			//			window.onscroll = function() {
			//				var y = document.documentElement.scrollTop || document.body.scrollTop;
			//				console.log('this is y' + y);
			//				if (y > oldy) {
			//					location.href = '#item-4';
			//					console.log('向下翻');
			//					oldy = y;
			//				} else if (y < oldy) {
			//					//		document.body.scrollTop-=fullheight;console.log('向上翻');oldy=y;
			//				} else {
			//					console.log('翻你妹啊');
			//					console.log('xiangdeng');
			//				}
			//
			//				console.log('this is oldy' + oldy);
			//			}
					var scrollFunc = function(e) {
							ee = e || window.event;
							var t1 = document.getElementById("wheelDelta");
							var t2 = document.getElementById("detail");
							var y = document.documentElement.scrollTop || document.body.scrollTop;
							var fullheight = document.body.offsetHeight;
							if (e.wheelDelta) { //IE/Opera/Chrome 
								var a = e.wheelDelta;//向上为120,向下为-120
								if(a>0){//向上	
								document.body.scrollTop -= fullheight/2;
								} 
								if(a<0){//向下
									document.body.scrollTop += fullheight/2;
								}
							} else if (e.detail) { //Firefox 
								var a = e.detail;//向上为-3,向下为3
								if(a<0){//向上	
								document.documentElement.scrollTop -= fullheight/2;
								} 
								if(a>0){//向下
									document.documentElement.scrollTop += fullheight/2;
								}
							}
							
							
							
							
						}
						
						/*注册事件*/
					if (document.addEventListener) {
						document.addEventListener('DOMMouseScroll', scrollFunc, false);
					} //W3C 
					window.onmousewheel = document.onmousewheel = scrollFunc; //IE/Opera/Chrome
		</script>
	</body>

</html>


源码如上。

一开始希望window.onscroll = function() {}的方法来实时检测滚动栏是否有变化。也实现了翻页的功能,但是直接就是一翻到底。。。也就是因为在实现翻页的时候由于document.body.scrollTop的变化,会自动触发新的window.onscroll函数。所以无奈只能求助百度,最后居然让我发现一个绝妙的方法和博客。不过没有做过向下的兼容性测试,最新版chrome和firefox是支持的。
希望自己可以慢慢学习,每天练习。
if (document.addEventListener) {
document.addEventListener('DOMMouseScroll', scrollFunc, false);
} //W3C
window.onmousewheel = document.onmousewheel = scrollFunc; //IE/Opera/Chrome
事件监听的兼容写法
最后一个需要注意的地方是获取当前页面顶端到body顶端的距离(比如你在第三页距离文档头部的距离为两个屏幕的高度),要这样写
var y=document.documentElement.scrollTop || document.body.scrollTop;谷歌中
document.documentElement.scrollTop怎么搞都是0,需要用bodywindow.event.wheelDelta非firefox浏览器获取到滚动条变化的情况,以谷歌为例。

而且上述代码的关键在于其中firex和其他浏览器的效果不同

document.body.scrollTop的变化,会自动触发新的window.onscroll函数。所以无奈只能求助百度,最后居然让我发现一个绝妙的方法和博客。不过没有做过向下的兼容性测试,最新版chrome和firefox是支持的。

如果向上滚动鼠标滚轮,则window.event.wheelDelta为120,向下则为-120.

火狐则是window.event.detail代表鼠标滚轮运动情况,且向上滚动window.event.detail的值为-3,向下为3.这是区别的地方 


文档也是乱乱的,最终效果也有点不理想。滚动起来不平滑,但是不用jquery的情况下写动画还是不会写。留待以后回过头复习的时候完善。
演示地址
https://github.com/Fucntion/jseveryday/blob/master/207/207.html



放一个自己最近做的小东西 米单词,公益背单词


document.body.scrollTop的变化,会自动触发新的window.onscroll函数。所以无奈只能求助百度,最后居然让我发现一个绝妙的方法和博客。不过没有做过向下的兼容性测试,最新版chrome和firefox是支持的。
在Vue中实现使用鼠标控制页面上下滑动,你可以通过添加滚动事件监听和设置页面滚动位置来实现。以下是一个示例: ```html <template> <div class="container" @wheel="handleScroll"> <!-- 页面内容 --> </div> </template> <script> export default { methods: { handleScroll(event) { // 获取鼠标滚轮滚动的方向,event.deltaY表示垂直方向上的滚动距离 const direction = event.deltaY > 0 ? 1 : -1; // 获取当前页面滚动的位置 const currentPosition = window.pageYOffset; // 设置新的页面滚动位置 window.scrollTo({ top: currentPosition + direction * 100, // 滚动距离,可以根据需要调整 behavior: 'smooth' // 平滑滚动效果 }); } } }; </script> <style scoped> .container { height: 100vh; /* 设置容器高度为视窗的高度 */ overflow-y: scroll; /* 允许垂直滚动 */ } </style> ``` 在这个示例中,我们给容器元素绑定了`@wheel`事件监听,即鼠标滚轮事件。当鼠标滚轮滚动时,会触发`handleScroll`方法。 在`handleScroll`方法中,我们首先判断鼠标滚轮滚动的方向,根据`event.deltaY`的值来判断。然后获取当前页面滚动的位置`window.pageYOffset`,并根据滚动方向和滚动距离的倍数来计算新的滚动位置。 最后,使用`window.scrollTo`方法设置新的页面滚动位置。其中,`top`属性表示距离页面顶部的距离,根据当前位置和滚动方向来计算。`behavior`属性设置为`smooth`表示使用平滑滚动效果。 通过以上代码,你可以实现在Vue中使用鼠标控制页面的上下滑动效果。记得将代码适配到你的Vue组件中。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值