如何在移动浏览器上获取pageYoffset

时间:2015-10-26 18:57:39

标签: javascript jquery html mobile

我需要跟踪网站上的页面偏移量。

var supportPageOffset = window.pageXOffset !== undefined;
var isCSS1Compat = ((document.compatMode || "") === "CSS1Compat");
var Y = supportPageOffset ? window.pageYOffset : isCSS1Compat ? document.documentElement.scrollTop : document.body.scrollTop;

使用上面的代码在所有桌面浏览器上返回我的垂直偏移量。但是,我在移动浏览器上只获得0。如何在移动设备上获取滚动位置?

1 个答案:

答案 0 :(得分:1)

我想这可能会对你有所帮助:

<强> Demo : Fiddle

<强> JQuery的

$(window).scroll(function (event) {
    var scroll = $(window).scrollTop(); // This would give you offsetY while you scroll
    $(".scrollY").text(scroll); // This is just to display for instance.
});

<强> Open in Mobile

更新

<强>的JavaScript

&#13;
&#13;
window.addEventListener('scroll', function(){
	var scrollTop = (window.pageYOffset !== undefined) ? window.pageYOffset : (document.documentElement || document.body.parentNode || document.body).scrollTop;
  
  document.getElementsByClassName("scrollY")[0].innerHTML = scrollTop;
  
}, true)
&#13;
.scrollY
{
    position:fixed;
    top:0px;
    left:0px;
    padding:10px;
    display:block;
    width:auto;
    height:auto;
    background:red;
    color:yellow;
}
.title h1
{
    text-align:center;
}

div
{
    display:block;
    width:90%;
    background:#2b2937;
    color:#eee;
    padding:10px;
    margin:10px 0px;
    border-radius:10px;
}
&#13;
<div class="scrollY"> </div>
<div class="title">
    <h1>Dummy HTML Text</h1>
</div>
<div>
    <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus</p>
</div>
<div>
    <h1>HTML Ipsum Presents</h1>
	       
<p><strong>Pellentesque habitant morbi tristique</strong> senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. <em>Aenean ultricies mi vitae est.</em> Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, <code>commodo vitae</code>, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. <a href="#">Donec non enim</a> in turpis pulvinar facilisis. Ut felis.</p>

<h2>Header Level 2</h2>
	       
<ol>
   <li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li>
   <li>Aliquam tincidunt mauris eu risus.</li>
</ol>

<blockquote><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus magna. Cras in mi at felis aliquet congue. Ut a est eget ligula molestie gravida. Curabitur massa. Donec eleifend, libero at sagittis mollis, tellus est malesuada tellus, at luctus turpis elit sit amet quam. Vivamus pretium ornare est.</p></blockquote>

<h3>Header Level 3</h3>

<ul>
   <li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li>
   <li>Aliquam tincidunt mauris eu risus.</li>
</ul>

<pre><code>
#header h1 a { 
	display: block; 
	width: 300px; 
	height: 80px; 
}
</code></pre>
</div>
&#13;
&#13;
&#13;

<强> Demo : Fiddle