简体   繁体   English

如何从 SharePoint Web 部件访问 onresize 事件?

[英]How do I access the onresize event from a SharePoint Web Part?

I have some JavaScript that will execute within a SharePoint web part.我有一些将在 SharePoint Web 部件中执行的 JavaScript。 I would like to have a function execute when the window is resized.我想在调整窗口大小时执行一个函数。 Normally I would use通常我会使用

<html>
  <body onresize="resizeFunction();">
  </body>
</html>

but in SharePoint things start to get hairy.但在 SharePoint 中,事情开始变得棘手。 I have an onload function that I am able to use thusly:我有一个onload函数,我可以这样使用:

_spBodyOnLoadFunctionNames.push('myFunctionName');

and this gets executed in the onLoad event.这在onLoad事件中执行。 Is there something similar for onResize events? onResize事件有类似的东西吗?

You might consider using an external javascript library such as jQuery or prototype, etc.您可能会考虑使用外部 javascript 库,例如 jQuery 或原型等。

For example, in jQuery, you might try (untested code!!)例如,在 jQuery 中,您可以尝试(未经测试的代码!!)

$("window").bind("resize", "myFunctionName");

jQuery docs: http://docs.jquery.com/Events/bind jQuery 文档: http : //docs.jquery.com/Events/bind

info on using jQuery in SharePoint: http://weblogs.asp.net/jan/archive/2008/11/20/sharepoint-2007-and-jquery-1.aspx在 SharePoint 中使用 jQuery 的信息: http : //weblogs.asp.net/jan/archive/2008/11/20/sharepoint-2007-and-jquery-1.aspx

EDIT: If you'd rather not use an external library, on document load run a function that attaches another function to the resize event.编辑:如果您不想使用外部库,则在文档加载时运行一个将另一个函数附加到 resize 事件的函数。 Something like:就像是:

_spBodyOnLoadFunctionNames.push(function() {
    window.onresize = function() {
         /* resize code here */
    }});

You could of course use named functions.您当然可以使用命名函数。

EDIT 2: Defining your events as above (window.onresize = ) is only good for one event.编辑 2:如上定义您的事件 (window.onresize = ) 仅适用于一个事件。 So if a multiple event handlers are specified the same way for the same event on the same page, whichever one is specified last "wins."因此,如果为同一页面上的同一事件以相同的方式指定了多个事件处理程序,则以最后一个指定的方式“获胜”。 The "correct" way to do it is to attach an event handler, and the easiest way to accomplish this is through a library, as they handle cross-browser differences transparently.执行此操作的“正确”方法是附加事件处理程序,完成此操作的最简单方法是通过库,因为它们透明地处理跨浏览器差异。

$(window).resize(function(){                
        alert("Ya its workingggg")  ;       
});

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

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