[英]Change position of tooltip on window resize
I'm using jQuery UI to create a tooltip for a search input field. 我正在使用jQuery UI为搜索输入字段创建工具提示。 I then want to position the tooltip according to the size of the browser window (top if less than 768px, left if more).
然后,我想根据浏览器窗口的大小来定位工具提示(如果小于768px,则为顶部,如果大于768px,则为左侧)。
I initialise the tooltip with: 我用以下命令初始化工具提示:
$('#search').tooltip({'placement':'top'});
Then I have this function to change the placement depending on the window size: 然后,我可以使用此功能根据窗口大小更改放置位置:
$(window).on('resize', function() {
if ($(window).width < 768) {
$("#damSearch").tooltip({'placement':'top'});
} else {
$("#damSearch").tooltip({'placement':'left'});
}
}).trigger('resize');
For some reason it's not working. 由于某种原因,它不起作用。 The tooltip initialises fine but when I resize the browser above 768px it still appears positioned to the top.
工具提示可以很好地初始化,但是当我将浏览器的大小调整为768px以上时,它仍显示在顶部。
[EDIT] [编辑]
I've been away for a few days and have just come back to try and resolve this problem. 我已经离开了几天,只是回来尝试解决这个问题。 I've installed Modernizr because I intend using it elsewhere on the site and so I thought I'd use Modernizr.mq to detect the window resizing.
我已经安装了Modernizr,因为我打算在网站上的其他地方使用它,所以我认为我应该使用Modernizr.mq来检测窗口的大小。 I also read elsewhere that the code to reposition the tooltip should be in its own self contained function, so this is the function:
我还在其他地方读到,用于重新定位工具提示的代码应位于其自身的自包含函数中,因此该函数是:
function positionTooltip() {
if (Modernizr.mq('(min-width: 768px)')) {
$("#damSearch").tooltip({'placement':'left'});
} else {
$("#damSearch").tooltip({'placement':'bottom'});
}
}
This is then followed in my Javascript file with: 然后在我的Javascript文件中添加:
$(document).ready(function() {
positionTooltip();
// Fire the function on page load
$(window).resize(positionTooltip);
// Fire function on window resize event
Unfortunately it's still not working correctly. 不幸的是,它仍然无法正常工作。 The tooltip appears correctly positioned when the page is first loaded, but if I then resize the browser the position is not updated.
首次加载页面时,工具提示的位置正确,但是如果我调整浏览器的大小,则位置不会更新。 If I reload the page however the tooltip's position is changed accordingly.
如果我重新加载页面,则工具提示的位置会相应更改。
It's as if the resize event is not triggering the function. 好像resize事件没有触发该函数。
[/EDIT] [/编辑]
As ever all help and advice is greatly appreciated. 一如既往,所有帮助和建议都将不胜感激。 Tony.
托尼
you need to call the width function 您需要调用width函数
if ($(window).width() < 768) {
notice the parentheses ()
注意括号
()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.