0

我在下面有完整的 jQuery 代码,它使用 UI 位置插件根据偏移量等在网页中定位图标。

虽然这个工作正常,但我在调整浏览器大小时遇到​​了一些问题,并且没有重新计算图标位置。如何为下面的代码将 .position() 函数绑定到 $(window).resize() 事件?

(function($) {
$(window).load(function(event) {


if ( $(element_selector).length !== 0 && $(title_class_selector).length !== 0  ) {

$(title_class_selector).position({
my: "center",
at: cornerPosition,
    offset:"<?php echo $x_coordinates_foradjustment;?> <?php echo $y_coordinates_foradjustment;?>",
of: $(element_selector)

});
}

    });
})(jQuery);

我试过把:

$(window).bind('resize',event);

就在关闭 jQuery 之前,它没有帮助。感谢您的任何提示..

4

1 回答 1

3

我建议您将位置处理移至单独的函数,然后将该函数绑定到loadresize事件:

var doPosition = function(e) {
    if ( $(element_selector).length !== 0 && $(title_class_selector).length !== 0  ) {

        $(title_class_selector).position({
            my: "center",
            at: cornerPosition,
            offset:"<?php echo $x_coordinates_foradjustment;?> <?php echo $y_coordinates_foradjustment;?>",
            of: $(element_selector)

        });
    }
};

$(window)
.load(doPosition)
.resize(doPosition);

注意:我强烈建议你对 resize 事件进行去抖动,否则它会触发很多次。这意味着当用户停止调整大小时,该事件将在调整大小期间触发一次而不是多次。在此处查看更多信息http://www.hnldesign.nl/blog/debouncing-events-with-jquery/

我创建了一个简单的小提琴(使用警报而不是你的位置计算东西,以显示它有效),去抖动,在这里

于 2013-04-10T07:42:58.937 回答