简体   繁体   English

jQuery on('resize')不能按预期工作

[英]jQuery on('resize') not working as expected

I wrote some jQuery to dynamically position an element. 我写了一些jQuery来动态定位元素。 While it works on load, it doesn't work when the window is resized. 虽然它在加载时起作用,但是在调整窗口大小时它不起作用。

The code I have: 我有的代码:

  $(document).ready(function() {
      var offset = $("#kDropdown").offset();
      //$(".hidden-dropdown").css('position', 'absolute')
      //$(".hidden-dropdown").css('left', offset.left);
      $(document).on('resize', function () {
        $(".hidden-dropdown").css('position', 'absolute')
        $(".hidden-dropdown").css('left', offset.left);
      }).trigger('resize');
    });


      <div class="hidden-dropdown hide">
        <ul>
          <li><a href="#">Item One</a></li>
          <hr />
          <li><a href="#">Item Two</a></li>
          <hr />
          <li><a href="#">Item Three</a></li>
        </ul>
      </div>

Any ideas? 有任何想法吗?

Usually, the one resized is the window , hence: 通常,调整大小的是window ,因此:

$(window).on("resize", function() {
    console.log("yay");
});

You can attach it with the resize method directly: 您可以直接使用resize方法将其附加:

$(window).resize(function() {
    console.log("direct yay");
});

They are both the same. 他们都是一样的。

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

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