View the scrolling distance of the scroll bar
Compatibility It’s quite confusing, taking two values and adding them at the same time, because it is impossible to have two values at the same time
IE8 and below are not compatible
window.pageXOffset/pageYOffset
<script type="text/javascript"> function getScrollOffset() { if(window.pageXOffset) { x : window.pageXoffset, y : window.pageYoffset } else{ return { x : document.body.scrollLeft + document.documentElement.scrollLeft, y : document.body.scrollTop + document.documentElement.scrollTop, } } } </script>
<script type="text/javascript"> function getSViewportOffset() { if(window.innerWidth) { return { w : window.innerWidth, h : window.innerHeight } }else{ if(document.compatMode ==="BackCompat") { return { w : document.body.clienWidth, h : document.body.clientHeight } }else{ return { w : document.documentElement.clientWidth, h : document.documrntElement.clientHeight } } } </script>
Let the scroll bar scroll to the current position instead of accumulating distance (these two methods are Exactly the same)
Make a small reader that will automatically turn pages.
<!DOCTYPE html><html><head> <title>Document</title></head><body>文本内容 <p style="width:100px;height:100px;background-color:orange;color:#fff;font-size:40px;text-align:center;line-height:100px;position:fixed;bottom:200px;right:50px;opcity:0.5;">start</p> <p style="width:100px;height:100px;background-color:orange;color:green;font-size:40px;text-align:center;line-height:100px;position:fixed;bottom:50px;right:50px;opcity:0.5;">stop</p></body><script type="text/javascript"> var start = document.getElement.getElementsByTagName('p')[0]; var stop = document.getElement.getElementsByTagName('p')[1]; var timer = 0; var key = true; //加锁,防止连续点start产生累加加速 start.onclick = function() { if(key) { timer = setInterval(function() { window.scollBy(0,10); },100); key = false; } } stop.onclick = function() { clearInterval(timer); key = true; }</script>
The above is the detailed content of Get window properties (viewport height, element size, element position) in JavaScript. For more information, please follow other related articles on the PHP Chinese website!