我在下面完全使用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之前,它没有帮助。 谢谢你的任何提示..
答案 0 :(得分:3)
我建议你将位置处理移到单独的函数,然后将该函数绑定到load
和resize
事件:
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);
注意:我强烈建议您去除调整大小事件,否则会多次触发。这意味着当用户停止调整大小时,事件将在调整大小期间触发一次而不是多次。点击此处http://www.hnldesign.nl/blog/debouncing-events-with-jquery/
我创建了一个简单的小提琴(使用警告代替你的位置计算东西,以显示它有效),并进行去抖动,over here