简体   繁体   English

刷新后 jQuery window.resize 不起作用

[英]jQuery window.resize doesn't work after refresh

<div id="visibility" class="image col-md-7 col-sm-7">
  <img class="graphics img-responsive" src="http://placehold.it/400x300">
  <img class="graphics img-responsive" src="http://placehold.it/400x300">
</div>  
<div id="hidden" class="media-carousel">
   <div class="responsive">
     <div><img class="graphics img-responsive" src="http://placehold.it/400x300"></div>
     <div><img class="graphics img-responsive" src="http://placehold.it/400x300"></div>
     <div><img class="drawing img-responsive" src="http://placehold.it/400x300"></div>
     <div><img class="drawing img-responsive" src="http://placehold.it/400x300"></div>
   </div>

    $(window).on('resize', function () {
    if ($(window).width() >= 767) {
        $('#hidden').css('display', 'none');
        $('#visibility').css('display', 'block');
        $('#none').css('display', 'none');
        $('#block').css('display', 'block');
    } else {
        $('#hidden').css('display', 'block');
        $('#visibility').css('display', 'none');
        $('#none').css('display', 'block');
        $('#block').css('display', 'none');
    }
});

hi, I have a problem with this script, specifically.嗨,我有这个脚本的问题,特别是。 I would like the element with id = "visibility" to have the display: none property when the browser window is> 767px, and an element has appeared on the page with id = "hidden" with the display: block property我希望 id = "visibility" 的元素在浏览器窗口大于 767px 时具有 display: none 属性,并且页面上出现了一个 id = "hidden" 且具有 display: block 属性的元素

When reduce browser window in developers tools it works, but when I refresh the page, the script does not work在开发人员工具中减少浏览器窗口时它可以工作,但是当我刷新页面时,脚本不起作用

Does anyone have any idea why ??有谁知道为什么?

thanks for the help谢谢你的帮助

This does not answer the question, but it will solve your problem much easier.这不能回答问题,但可以更轻松地解决您的问题。 Use media queries like this in your CSS file:在 CSS 文件中使用这样的媒体查询:

#hidden { display: none; }
#visibility { display: block; }
#none { display: none; }
#block { display: block; }

@media only screen and (max-width: 767px) {
  #hidden { display: block; }
  #visibility { display: none; }
  #none { display: block; }
  #block { display: none; }
}

Media query will "switch" your css styles, in case the width of your screen is less than 767px媒体查询将“切换”您的 css 样式,以防您的屏幕宽度小于 767px

this thing also happened to me i just tried this这件事也发生在我身上,我刚试过这个

      $(window).on('load resize ', function () {

       //your code 
       })

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

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