当滚动页面滚动准确地发生在其中的块的高度(代码在里面)时,如何做?
我不想使用这个库,因为很可能需要添加2-5行代码来解决滚动页面在块高度滚动时的问题(预定数量的像素)。
第二个问题是如何使这是一个顺利的滚动,这不是那种感觉,只是从一个单元切换到另一个。
function slide() {
h = document.documentElement.clientHeight
$(".one, .two, .three").css('height', h);
};
$(window).resize(slide);
$(document).ready(slide);.one,
.two,
.two {
display: block;
position: relative;
width: 100%;
}
.one {
background: #CD5;
}
.two {
background: aquamarine;
}
.three {
background: #2196F3;
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div class="one"></div>
<div class="two"></div>
<div class="three"></div>
发布于 2016-03-03 07:19:43
将您的滚动处理程序绑定到鼠标轮和DOMMouseScroll事件,并使用(event.originalEvent.wheelDelta >0欧元/ event.originalEvent.detail < 0)来确定滚动的方向。然后使用$().offset().top找到要滚动的div顶部,使用$.animate()进行滚动。
function slide() {
h = document.documentElement.clientHeight
$(".one, .two, .three").css('height', h);
};
$(window).resize(slide);
$(document).ready(slide);
$(document).bind('mousewheel DOMMouseScroll', function(event) {
scroll(event);
});
var num = 1;
var scrolling = false;
function scroll(event) {
event.preventDefault();
if (!scrolling) {
scrolling = true;
if (event.originalEvent.wheelDelta > 0 || event.originalEvent.detail < 0) {
num--;
num = num < 1 ? 1 : num;
} else {
num++;
num = num > 3 ? 3 : num;
}
$('html, body').animate({
scrollTop: $(".num" + num).offset().top
}, 500, "linear", function() {
scrolling = false;
});
}
}.one,
.two,
.two {
display: block;
position: relative;
width: 100%;
}
.one {
background: #CD5;
}
.two {
background: aquamarine;
}
.three {
background: #2196F3;
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div class="one num1"></div>
<div class="two num2"></div>
<div class="three num3"></div>
发布于 2016-03-03 07:00:11
根据我的理解,我已经创建了一个解决方案,请检查https://jsfiddle.net/v7ok83oa/。
我用的是鼠标轮事件。
<div class="one">
<h1>ONE</h1></div>
<div class="two">
<h1>Two</h1></div>
<div class="three">
<h1>Three</h1></div>CSS
* {
margin: 0;
padding: 0
}
.one,
.two,
.three {
display: block;
position: relative;
width: 100%;
overflow: auto;
}
.one {
background: #CD5;
}
.two {
background: aquamarine;
}
.three {
background: #2196F3;
}
h1 {
color: white;
text-align: center;
}jQuery
$(document).ready(function() {
var h = $(document).height();
var body = $("body");
$(".one, .two, .three").css('height', h);
$(document).on('mousewheel', function(e) {
if (e.originalEvent.wheelDelta / 120 < 0) { // if Mouse wheel up
var st = $(document).scrollTop();
body.animate({
scrollTop: st + h
}, '500');
} else if (e.originalEvent.wheelDelta / 120 > 0) { // if Mouse wheel down
var st = $(document).scrollTop();
body.animate({
scrollTop: st - h
}, '500');
}
console.log($('.one').height());
});
});更新检查此https://jsfiddle.net/v7ok83oa/3/
如果你在动画结束前快速滚动事件触发器,我已经修复了它。
https://stackoverflow.com/questions/35764232
复制相似问题