简体   繁体   English

ScrollLeft ScrollTop,它们如何工作?

[英]ScrollLeft ScrollTop, How do they work?

I'm trying to make some of my element fixed for vertical and horizontal scrolling and looked up some examples on stackoverflow but I could not figure out why they work, therefore I cannot use it for a different purpose: This is the code that everybody gives, and it works: 我试图将某些元素固定为垂直和水平滚动,并在stackoverflow上查找了一些示例,但我无法弄清它们为什么起作用,因此无法将其用于其他目的:这是每个人提供的代码,并且有效:

$(window).scroll(function() {
   $('#header').css({
      'top': $(this).scrollTop() + 15,
      'left': $(this).scrollLeft() + 15   // top, left = 15px in css
   });      
}

So what it is doing is set the position of top and left of the header id tag so everytime window is scrolled, it goes to the position relative to window? 那么,这是在设置标头id标记的顶部和左侧的位置,以便每次滚动窗口时,它都转到相对于窗口的位置吗?

$(this).scrollTop() always print out 0 in my test however, if I do the below instead, it stop working: $(this).scrollTop()在测试中始终显示为0,但是,如果我执行以下操作,它将停止工作:

function test() { /* Original code example, keeping this unmodified so some answers doesn't seem strange */
   $('#header').css({
      'top': 15,
      'left': 15 // hardcode 15 just for example
   });      
}

What is the purpose of $(this).scrollTop() here that makes or breaks the functionality? $(this).scrollTop()在这里实现或破坏功能的目的是什么?

Lastly, I'm not allowed to use JQuery so I use javascript and none of these variations are working. 最后,不允许使用JQuery,所以我使用javascript,但这些变体均无效。 Could you tell me what I'm doing wrong? 你能告诉我我在做什么错吗?

function test() {
   var header = getElementById('header');
   header.style.top = header.scrollTop + 15;   
   header.style.left = header.scrollLeft + 15;   
}

also tried few others such as: using '15px', 15 + 'px'. 还尝试了一些其他方法,例如:使用'15px',15 +'px'。

edit: modified first code example to the correct original code from stackoverflow 编辑:将第一个代码示例修改为来自stackoverflow的正确原始代码

top and left property of css not going to work in this condition if you do not add position property .. 如果不添加position属性,则css的top和left属性在这种情况下将不起作用。

function test() {
   $('#header').css({
      'position':'absolute',
      'top': $(this).scrollTop() + 215,
      'left': $(this).scrollLeft() + 15   // top, left = 15px in css
   });      
}

LIVE http://jsfiddle.net/mailmerohit5/t45ktx4r/ 直播 http://jsfiddle.net/mailmerohit5/t45ktx4r/

The getElementById function is not global on its own -- it is under the global document object. getElementById函数本身不是全局的,而是位于全局document对象下。 So you must use var header = document.getElementById('header'); 因此,您必须使用var header = document.getElementById('header');


Next, the browser's native scrollTop and scrollLeft functions used on a DOM element will tell you the scroll inside that element. 接下来,在DOM元素上使用的浏览器本机的scrollTopscrollLeft函数将告诉您该元素内的滚动 For instance, for scrollTop is the measurement from the top of the element (which may be hidden because of scroll) to the top visible edge (In other words, it is the distance above the visible part of the element -- the part that's been hidden because of internal scrolling). 例如,对于scrollTop是从元素顶部(可能由于滚动而被隐藏)到顶部可见边缘(换言之,它是元素可见部分上方的距离)的测量值。隐藏(因为内部滚动)。 If the top of the header element is always completely visible (and the header doesn't have it's own scrollbar), then this distance will always be zero. 如果header元素的顶部始终是完全可见的(并且header没有它自己的滚动条),则此距离将始终为零。

To position the header relative to the scroll of the browser window, you may want to get the pageYOffset and pageXOffset on the window element instead, and then position the header based on that. 要相对于浏览器窗口的滚动条定位标题,您可能希望改为在window元素上获取pageYOffset和pageXOffset,然后基于此元素定位标题。 For instance, window.pageYOffset + 15. 例如, window.pageYOffset + 15.

Here is a safer alternative that checks various possible places for the page's scroll (helps with browser compatibility). 这是一种更安全的替代方法,它检查页面滚动的各种可能位置(有助于浏览器兼容性)。

var scrollTop = function(){
    return (window.pageYOffset !== undefined) ? window.pageYOffset : (document.documentElement || document.body.parentNode || document.body).scrollTop;
}

var scrollLeft = function(){
    return (window.pageXOffset !== undefined) ? window.pageXOffset : (document.documentElement || document.body.parentNode || document.body).scrollLeft;
}

The last thing is that element.style.top should be given a string value that is like "10px" , so you will need to do the calculation first and then add + 'px' at the end. 最后一件事是应该给element.style.top一个类似于"10px"的字符串值,因此您需要首先进行计算,然后在末尾添加+'px'。

eg. 例如。 header.style.top = window.scrollTop + 15 + "px";


Putting that all together: 放在一起:

var scrollTop = function(){
    return (window.pageYOffset !== undefined) ? window.pageYOffset : (document.documentElement || document.body.parentNode || document.body).scrollTop;
}

var scrollLeft = function(){
    return (window.pageXOffset !== undefined) ? window.pageXOffset : (document.documentElement || document.body.parentNode || document.body).scrollLeft;
}

function test() {
   var header = document.getElementById('header');
   header.style.top = scrollTop() + 15 + 'px';   
   header.style.left = scrollLeft() + 15 + 'px';   
}

Good luck! 祝好运! Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM