简体   繁体   English

不同屏幕尺寸的Javascript

[英]Javascript on different screen sizes

i need to let this script run on screen sizes with a width of 794 px or higher. 我需要让此脚本在宽度为794像素或更高的屏幕上运行。 For smaller screen sizes it should not run, because this makes problems with another script. 对于较小的屏幕,它不应运行,因为这会使另一个脚本产生问题。

i have tried different stuff, but i don't really know how to make this happen. 我尝试了不同的东西,但我真的不知道该如何实现。

jQuery(document).ready(function ($) {
    $('a[href*=#]:not([href=#])').click(function () {
        if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
            var target = $(this.hash);
            target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
            if (target.length) {
                $('html,body').animate({
                    scrollTop: target.offset().top
                }, 1000);
                return false;
            }
        }
    });
});

Can anyone let me know, how i can adjust this to let this script just run on screen width 794 px or higher? 谁能告诉我,我如何调整它以使此脚本仅在794像素或更高的屏幕宽度上运行?

Best regards Safak 最好的问候萨法克

You can use window.matchMedia() for media queries in javascript. 您可以使用window.matchMedia()在javascript中进行媒体查询。

for example 例如

var mq = window.matchMedia( "(max-width: 570px)" );
if (mq.matches) {
    // window width is at less than 570px
}
else {
    // window width is greater than 570px
}

For web browser support : Please refer " https://hacks.mozilla.org/2012/06/using-window-matchmedia-to-do-media-queries-in-javascript/ " 对于网络浏览器支持 :请参阅“ https://hacks.mozilla.org/2012/06/using-window-matchmedia-to-do-media-queries-in-javascript/

Update 更新

If you want to run the script only once and not on resizing you can use 如果您只想运行一次脚本而不是重新调整大小,则可以使用

if($(window).width()>=794)
{
    //do your stuffs here
}

With jQuery: 使用jQuery:

$(function(){
    $( window ).resize(function(){
        if( $( window ).width() >= 794 ){
            // Your code here
        }
    });   
    $( window ).resize();    // Trigger window resize to check on load
});

You can use window.width() 您可以使用window.width()

ex: 例如:

if(!$(window).width()<794)
{
    //do your stuffs here
}

use $(window).width() 使用$(window).width()

if($(window).width() >= yourScreenSize)
{
   // your code goes here
}

A non jQuery solution would look something like this 非jQuery解决方案看起来像这样

if (window.innerWidth > 794) {
   myFunc(); //the code that you want to run on bigger screens
}
jQuery(document).ready(function ($) {
$('a[href*=#]:not([href=#])').click(function () {
    if( $(window).width() >= 794 )
    {
    if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
        var target = $(this.hash);
        target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
        if (target.length) {
            $('html,body').animate({
                scrollTop: target.offset().top
            }, 1000);
            return false;
        }
    }
    }//window size check ends here
});

}); });

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

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