[英]Stop scrolling when div reaches fixed position on the page
I am trying to stop scrolling when a DIV is in view and centered in the viewport as the user scrolls down the page. 当用户在页面上向下滚动时,在试图查看DIV并在视口居中时,我试图停止滚动。
When the page is stopped and the user scrolls I need the content of the DIV to scroll horizontally and then allow the user to continue scrolling. 当页面停止并且用户滚动时,我需要DIV的内容水平滚动,然后允许用户继续滚动。
function pauseScroll() {
// $(document).bind('mousewheel DOMMouseScroll', function() {
disableScroll();
console.log('trying to scroll Card: ' + selCard);
setTimeout(pauseStop(), 500);
slideCard(selCard);
// });
}
function pauseStop() {
console.log('Pause Stop');
}
function unpauseScroll() {
// $(document).unbind('mousewheel DOMMouseScroll');
enableScroll();
document.getElementById("status").innerHTML = "enabled";
document.getElementById("status").className = "enabled";
}
// left: 37, up: 38, right: 39, down: 40,
// spacebar: 32, pageup: 33, pagedown: 34, end: 35, home: 36
var keys = {
37: 1,
38: 1,
39: 1,
40: 1
};
function preventDefault(e) {
e = e || window.event;
if (e.preventDefault)
e.preventDefault();
e.returnValue = false;
}
function preventDefaultForScrollKeys(e) {
if (keys[e.keyCode]) {
preventDefault(e);
return false;
}
}
function disableScroll() {
if (window.addEventListener) // older FF
window.addEventListener('DOMMouseScroll', preventDefault, false);
window.onwheel = preventDefault; // modern standard
window.onmousewheel = document.onmousewheel = preventDefault; // older browsers, IE
window.ontouchmove = preventDefault; // mobile
document.onkeydown = preventDefaultForScrollKeys;
}
function enableScroll() {
if (window.removeEventListener)
window.removeEventListener('DOMMouseScroll', preventDefault, false);
window.onmousewheel = document.onmousewheel = null;
window.onwheel = null;
window.ontouchmove = null;
document.onkeydown = null;
}
$(window).scroll(function() {
if ($("div").hasClass("cardstop")) {
var top_of_element = $(".cardstop").offset().top;
var bottom_of_element = $(".cardstop").offset().top + $(".cardstop").outerHeight();
var bottom_of_screen = $(window).height()
var top_of_screen = $(window).scrollTop();
var elHeight = bottom_of_element - top_of_element;
var topSpace = ((($(window).height()) - elHeight) / 2);
var scrollFix = top_of_screen + topSpace;
//top_of_screen
var st = $(this).scrollTop();
if (st > lastScrollTop) {
// downscroll code
console.log('Top of el: ' + top_of_element);
console.log('TopScreen: ' + top_of_screen);
console.log('Space: ' + topSpace);
console.log('Bot of el: ' + bottom_of_element);
console.log('BotScreen: ' + bottom_of_screen);;
if (top_of_element < scrollFix) {
if (selCard = 1) {
console.log('One to Two');
pauseScroll();
selCard = 2;
} else if (selCard = 2) {
pauseScroll();
selCard = 1;
}
unpauseScroll();
}
} else {
// upscroll code
console.log('Scroll Up: ');
if (selCard = 3) {
console.log('3 to 2');
pauseScroll();
selCard = 4;
}
if (selCard = 4) {
pauseScroll();
unpauseScroll();
}
}
lastScrollTop = st;
// if (bottom_of_screen > top_of_element) {
// The element is visible, do something
}
});
The process is kind of working apart it's scrolling the card content too quickly and then moving on. 该过程有点麻烦,因为它太快地滚动了卡片内容,然后继续前进。
Any pointers on where I am going wrong would be great. 任何关于我要去哪里的指示都很好。
Regards 问候
I've added a mockup on [jsfiddle]: https://jsfiddle.net/stato74/sjtp9wv3/2/ 我在[jsfiddle]上添加了一个模型: https ://jsfiddle.net/stato74/sjtp9wv3/2/
I would admit you to use this JS tool: Waypoints http://imakewebthings.com/waypoints/ 我会让您使用此JS工具:Waypoints http://imakewebthings.com/waypoints/
If you prefer the hard way, then you can set "html, body{overflow-y=hidden}" and set your Div {overflow-y=scroll} 如果您更喜欢硬方法,则可以设置“ html,body {overflow-y = hidden}”,并设置Div {overflow-y = scroll}
This will let you scroll the div instead of the whole page. 这将使您滚动div而不是整个页面。 If I will find some time later, I can provide code for this (if needed)
如果以后能找到我,可以为此提供代码(如果需要)
the maximum value of scrollTop
is not scrollHeight
, it's scrollHeight
- outerHeight
. scrollTop
的最大值不是scrollHeight
,它是scrollHeight
- outerHeight
。
This will give you the correct value: 这将为您提供正确的值:
var elem = $("body");
var maxScrollTop = elem[0].scrollHeight - elem.outerHeight();
$(document).scroll(function(){
if(e.target.scrollTop > maxScrollTop ){
e.target.scrollTop = maxScrollTop ;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.