jquery如何在调整大小时刷新页面?

时间:2010-06-16 09:37:04

标签: jquery jquery-ui

我有一些指示器显示设置浮动在菜单上方。问题是当有人调整“指标停留在同一个地方”时,页面需要“刷新到正确定位”。如何在检测到页面大小时自动刷新页面? 或者有更好的方法吗? 的的jQuery

    function findPosY(b) {
    var a = 0;
    if (b.offsetParent) {
        while (1) {
            a += b.offsetTop;
            if (!b.offsetParent) {
                break
            }
            b = b.offsetParent
        }
    } else {
        if (b.y) {
            a += b.y
        }
    }
    return a
}
function findPosX(b) {
    var a = 0;
    if (b.offsetParent) {
        while (1) {
            a += b.offsetLeft;
            if (!b.offsetParent) {
                break
            }
            b = b.offsetParent
        }
    } else {
        if (b.x) {
            a += b.x
        }
    }
    return a
}
function setNotifications() {
    $("#shortcut_notifications span").each(function () {
        if ($(this).attr("rel") != "") {
            target = $(this).attr("rel");
            if ($("#" + target).length > 0) {
                var a = findPosY(document.getElementById(target));
                var b = findPosX(document.getElementById(target));
                $(this).css("top", a - 31 + "px");//-24
                $(this).css("left", b + 83 + "px")//+60
            }
        }
    });
    $("#shortcut_notifications").css("display", "block")
}

CSS

#shortcut_notifications {
display:none;
}

.notification {
color:#fff;
font-weight:700;
text-shadow:1px 0 0 #333;
background:transparent url(../images/bg_notification.png) no-repeat center;
position: absolute;/*absolute*/
width:37px;/*37*/
height:37px;
display:block;
text-align:center;
padding-top:17px;
color:#ffffff;
}

#nav li {
    display:block;
    float:right;
    padding:19px 21px;
    text-align: left;
    position:relative;
    height:24px;
    font-size:16px;
}

HTML

<li class="settings"><a class="settings" href="#">Settings</a>
        <ul>
          <li><a href="#">Game Settings</a></li>
          <li><a href="#">Transactions </a></li>
        </ul>
      </li>

3 个答案:

答案 0 :(得分:2)

您可以使用.resize()处理程序,如下所示:

$(window).resize(setNotifications);

答案 1 :(得分:1)

有一个可以挂钩的窗口调整大小事件。只需重新运行用于初始定位指标的代码,它应该都可以正常工作。

这个问题对使用resize事件有很好的答案:

Cross-browser window resize event - JavaScript / jQuery

答案 2 :(得分:0)

我认为这很有效。检查IE浏览器和Firefox。

$(窗口).bind(&#39;调整&#39;,函数(){

 window.location.href = window.location.href;

});

相关问题