SharePoint窗口滚动未触发

7

我有一个由SharePoint页面生成的HTML(剪辑):

<body scroll="yes" onload="if (typeof(_spBodyOnLoadWrapper) != 'undefined') _spBodyOnLoadWrapper();" class="v4master" style="overflow: scroll" spellcheck="false">
    <form name="aspnetForm" method="post" action="/Lists/List/EditNewForm.aspx?ID=2&amp;Source=https%3A%2F%2Fsp2010-test%2Eatwss%2Ecom%2FLists%2FList%2FAllItems%2Easpx" onsubmit="javascript:return WebForm_OnSubmit();" id="aspnetForm" style="overflow: scroll">

// some html here

<div id="competenceTotalSum" style="position: absolute; left: 500px; top: 400px; width: 100px; height: 50px; background-color:gray" />
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
    $(function(){       
        $("form#aspnetForm").bind("scroll", function(e){
            alert("scroll");
            $("#competenceTotalSum").css("top", $(this).scrollTop() + 400);
        });
    });
</script>

// some html here

    </form>
</body>

事件 scroll 没有触发。我改变了 body 元素的 scroll 属性、bodyform 元素的溢出属性,尝试将 scroll 事件绑定到不同的对象(windowbodyform),但是当将 scroll 事件更改为 click 事件时,它就会触发。除了滚动元素的 overflow 属性外,我没有找到任何原因。

bind()已经被弃用,推荐使用"on" - 请参见此处:http://api.jquery.com/on - Sebastian
@Sebastian,谢谢你,但它并没有解决问题。使用on()也不起作用。 - David Levin
抱歉造成混淆 - 上面的评论只是对你的代码的一般建议。请看下面的答案获取实际答案。 ;) - Sebastian
@Sebastian,两个答案都没有帮到我。为了我的目的,我使用了$(window).resize事件和块的固定位置。 - David Levin
3个回答

23

虽然这是一个有点老的问题,但对其他人仍可能有帮助。我想在我的SharePoint项目中实现回到顶部的功能。

经过几个小时的尝试后,我用下面的代码让它正常运行了。

实际上,$(window).scroll()在SharePoint中不起作用,我使用主ID:('#s4-workspace')来实现滚动到顶部的功能。

$(document).ready(function () {
    var offset = 220;
    var duration = 1000;

  jQuery('#s4-workspace').scroll(function() {

        if (jQuery(this).scrollTop() > offset) {
            jQuery('.arrowToTop').fadeIn(duration);
        } else {
            jQuery('.arrowToTop').fadeOut(duration);
        }
    });

    jQuery('.arrowToTop a').click(function(event) {
        event.preventDefault();
       jQuery('#s4-workspace').animate({scrollTop: 0}, duration);
        return false;
    }) });

我已使用以下CSS样式

.arrowToTop {
   display: none;
   height: 100%;
   position: fixed;
   right: 20px;    
   z-index: 9999;
   bottom: 0;
   width: 70px;
   height:70px;
} 

.arrowToTop a{     
    width: 70px;
    height:70px; 
    display: block;
     background: url(../images/arrow.png) no-repeat left 0;
    }

2
哇,终于有一个可用的版本了。非常感谢你:D - Andy Allison

0

就我目前的看法,form#aspnetForm 不应该有可用的滚动条,对吧?

overflow: scroll 只有与 heightmax-height 一起使用才有意义。(此外,我会将 overflow: scroll 替换为 overflow: auto,这样你只会显示你实际需要显示的滚动条,最可能是垂直滚动条。

如果你想跟踪整个文档的滚动,将你的代码更改为

$("body").on("scroll", function(e){
    alert("scroll");
    $("#competenceTotalSum").css("top", $(this).scrollTop() + 400);
});

0
请检查以下代码,看看它是否能帮到您。我已经为表单添加了高度。
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body scroll="yes" onload="if (typeof(_spBodyOnLoadWrapper) != 'undefined') _spBodyOnLoadWrapper();"  style="overflow: scroll"  class="v4master" spellcheck="false">
    <form name="aspnetForm" method="post"  style="height:100px;overflow: scroll"  action="/Lists/List/EditNewForm.aspx?ID=2&amp;Source=https%3A%2F%2Fsp2010-test%2Eatwss%2Ecom%2FLists%2FList%2FAllItems%2Easpx" 
    onsubmit="javascript:return WebForm_OnSubmit();" id="aspnetForm"  >

// some html here<br>
// some html here<br>
// some html here<br>
// some html here<br>
// some html here<br>
// some html here<br>
// some html here<br>
// some html here<br><br>
// some html here<br>
// some html here<br>
// some html here<br>
// some html here<br>
// some html here<br>
// some html here<br>
// some html here<br>
// some html here<br>

<div id="competenceTotalSum" style="position: absolute; left: 500px; top: 400px; width: 100px; height: 50px; background-color:gray" >asdsa</div>

<script type="text/javascript">
    $(function(){       
        $("form#aspnetForm").bind("scroll", function(e){
            alert("scroll");
            $("#competenceTotalSum").css("top", $(this).scrollTop() + 400);
        });
    });
</script>

// some html here

    </form>
</body>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接