简体   繁体   English

JavaScript命令:»如果div宽度<500px ...«

[英]JavaScript command: »If div width <500px …«

I work with a mixture between jQuery Splitter and Masonry . 我使用jQuery SplitterMasonry之间的混合物。

A part of my code is: 我的代码的一部分是:

var splitter = $('#main_container').split({
orientation: 'vertical',
limit: 100,
position: s1+'px',
onDrag: function(event) {
setCookie("left", splitter.position() );
Splitter();
$('.grid').masonry({
itemSelector: '.grid-item',
percentPosition: true,
horizontalOrder: false
});
}
});

Splitter();
function Splitter(){
if( splitter.position() <500){
$('.grid-item').css("width", "100%");
$('#show_all').fadeOut();
}
else{
$('.grid-item').css("width", "33.3333%");
$('#show_all').fadeIn();
}
}

And I need a command like this: » If the container #container (with a flexible width) is <500px wide, the position has to be again on position: '50%' width the limit: 300 . 我需要一个这样的命令:» 如果容器#container (宽度灵活)宽度<500px,则位置必须再次位于:'50%'宽度限制:300 « «

How is that possible? 怎么可能? Would be so thankful for help! 会非常感谢你的帮助! <3 <3

// //

EDIT: Something like » Reload the page if #container is <500px wide « would be also okay. 编辑:像» 如果#container <500px宽 « 重新加载页面也可以。

I tried this: 我试过这个:

function refreshPage() {
if ($('#container').width() < 900);
}

But it does not work. 但它不起作用。

If you want to reload the page if the container is less than 500px wide try: 如果要在容器宽度小于500px时重新加载页面,请尝试:

setInterval(function(){
  if($("#container").width < 500){
    location.reload()
  }
}, 100);

This sets a timer to happen 10 times a second that checks if the container width is less than 500, if it is less than 500, it will reload the page. 这将计时器设置为每秒10次,检查容器宽度是否小于500,如果小于500,它将重新加载页面。

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

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