简体   繁体   English

设备视口大于1000px时如何运行jQuery代码

[英]How to run jQuery code when the device viewport is greater than 1000px

I have a functioning jQuery code which which I need to run only on devices with viewport larger than 1000px.我有一个可以运行的 jQuery 代码,我只需要在视口大于 1000px 的设备上运行它。 I am a beginner.我是初学者。 I put up this code to run only on the homepage but I can't get it to run for the said viewport.我将此代码仅在主页上运行,但无法为所述视口运行它。 I should probably also mention that I'm using this inside WordPress.我可能还应该提到我在 WordPress 中使用它。 I have added the php code inside my functions.php and works as it's supposed except for this bit.我在我的functions.php 中添加了php 代码,除了这一点外,它按预期工作。 If someone could help, thanks.如果有人可以帮忙,谢谢。

The code is as follows: jQuery(document).ready(function($){ $(window).on('scroll', function(){代码如下: jQuery(document).ready(function($){ $(window).on('scroll', function(){

var y = $(window).scrollTop();
if(window.location.pathname == '/'){
if( y > 0 ){
        $('#top').fadeIn();
        $('#header-space').fadeIn();
    } else {
        $('#top').fadeOut();
        $('#header-space').fadeOut();
    }
}
else {}

});
});

I made this attempt together with CSS but I failed:我和 CSS 一起做了这个尝试,但我失败了:

$(document).ready(function() {
// run test on initial page load
checkSize();

// run test on resize of the window
$(window).resize(checkSize);
});

//Function to the css rule
function checkSize(){
if ($(".sampleClass").css("float") == "none" ){
    if(window.location.pathname == '/'){

if( y > 0 ){
        $('#top').fadeIn();
        $('#header-space').fadeIn();
    } else {
        $('#top').fadeOut();
        $('#header-space').fadeOut();
    }

}
 else {}
}
}

MyCSS looks like this: Please don't mind my random class names for now. MyCSS 看起来像这样: 现在请不要介意我随机的类名。

.sampleClass {float:left;}
@media only screen and (min-width: 1000px){
.sampleClass {float:none;}
}

I'm completely stuck now.我现在完全被困住了。

Get the width of the window:获取窗口的宽度:

 var width = $(window).width();

Then you can play around like:然后你可以像这样玩:

if (width > 1000) {
// dosomething big
} else {
// stay low
}

暂无
暂无

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

相关问题 仅当视口宽度大于1000px时才运行WayPoint吗? - Only run WayPoints if viewport width is greater than 1000px? 如何为视口小于 1000px 且仅在主页上的设备运行 jQuery 代码 - How to run jQuery code for devices with a viewport of less than 1000px and only on the homepage 如何仅在 WordPress 中的视口大于 1000px 时执行一些 jquery 代码 - How to execute some piece of jquery code only when the viewport is above 1000px in WordPress Jquery 当动态 div 的高度大于 1000px 时显示/隐藏 div - Jquery Show/Hide div when dynamic div has height greater than 1000px 当设备宽度小于1000px时,jQuery的.height()函数返回0 - jQuery's .height() function returns 0, when device width is less than 1000px 如何在1000px以下显示脚本 - how to script show in below 1000px 如何禁用滚动条是身体的宽度和高度大于1000px和600px? - How to disable the scrollbars is width and height of body bigger than 1000px and 600px? 如果窗口宽度在 1000px 下超过 0.2 秒,是否可以重新加载网站? - Is it possible to reload a website if the window width is for more than 0.2 seconds under 1000px? 我想在1000px或更少的像素上触发此jQuery。 我可以用PHP做到吗? - I want to trigger this jQuery on 1000px or less. Can I do that with PHP? 什么是jQuery相当于@media屏幕和(max-width:1000px)和(min-width:747px) - What is the jQuery equivalent of @media screen and (max-width:1000px) and (min-width:747px)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM