想象一下这是我的页面:
<p>hello</p>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<p class="myPara">My Paragraph</p>
Run Code Online (Sandbox Code Playgroud)
当用户向下滚动到具有"myPara"类的段落而不是之前,我如何提醒消息?
这是一个证明问题的jsbin示例.
基本上,我有以下javascript将窗口滚动到页面上的锚点:
// get anchors with href's that start with "#"
$("a[href^=#]").live("click", function(){
var target = $($(this).attr("href"));
// if the target exists: scroll to it...
if(target[0]){
// If the page isn't long enough to scroll to the target's position
// we want to scroll as much as we can. This part prevents a sudden
// stop when window.scrollTop reaches its maximum.
var y = Math.min(target.offset().top, $(document).height() - $(window).height());
// also, don't …Run Code Online (Sandbox Code Playgroud) 我使用此代码滚动到我的页面上的某个元素:
$("html, body").animate({scrollTop: $(".myDiv").offset().top}, 300);
Run Code Online (Sandbox Code Playgroud)
它的工作原理,但有一个问题,它:当用户向下滚动而滚动剧本了,有一些颤动,因为有在不同的方向上同时有两个滚动命令 - 听起来逻辑性.
我检查了一些具有这种滚动功能的其他网站,没有动摇.那么防止这种情况的诀窍是什么?