简体   繁体   English

根据window宽度改变javascript

[英]Change javascript according to window width

I think this is quite simple but after 2 days of trying I'm still clueless.我认为这很简单,但经过 2 天的尝试,我仍然一无所知。 Basically, I need to run one set of commands if the screen is over 767 pixels wide and another if the screen is under 767 pixels.基本上,如果屏幕宽度超过 767 像素,我需要运行一组命令;如果屏幕宽度低于 767 像素,我需要运行另一组命令。

When the screen is wider than 767 pixels, I want to:当屏幕大于 767 像素时,我想:

<script type="text/javascript">

    var jsReady = false;//for flash/js communication

    // FLASH EMBED PART
    var flashvars = {};
    var params = {};

    params.quality = "high";
    params.scale = "noscale";
    params.salign = "tl";
    params.wmode = "transparent";
    params.bgcolor = "#111111";//change flash bg color here
    params.devicefont = "false";
    params.allowfullscreen = "true";
    params.allowscriptaccess = "always";
    var attributes = {};
    attributes.id = "flashPreview";

    swfobject.embedSWF("preview.swf", "flashPreview", "100%", "100%", "9.0.0", "expressInstall.swf", flashvars, params, attributes);

    <!-- and much more code... -->

</script>

When the screen is narrower than 768 pixels, I want to run:当屏幕小于 768 像素时,我想运行:

<script type="text/javascript">  

        jQuery(function($){
            $.supersized({
                //Background image
                slides  :  [ { image : 'img/some_image.jpg' } ]                 
            });
        });

</script>

That's right... For desktops and tablets, I want to show a full-screen video background.没错...对于台式机和平板电脑,我想显示全屏视频背景。 For smaller screens (less than 767 pixels), I want to show a single still image background.对于较小的屏幕(小于 767 像素),我想显示单个静止图像背景。

if(screen.width > 767) {
   code A...
} else {
   code B...
}

You can get the currect size of windows using $(window).width() and attach a handler on the resize event of the form.您可以使用$(window).width()获取 windows 的当前大小,并在表单的调整大小事件上附加一个处理程序。 For a simple use, It is as simple as对于简单的使用,它就像

$(window).resize(funcion() {
    $width = $(window).width();
    if($width < 767) {
            $.supersized({
                //Background image
                slides  :  [ { image : 'img/some_image.jpg' } ]                 
            });
    } else {
        //if width is greater than 767
    }
});

Since you're using JQuery you can use this:由于您使用的是 JQuery,因此您可以使用:

if ($(window).width > 767) { ... }

That returns the current size of the window, not the max.这将返回 window 的当前大小,而不是最大值。

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

相关问题 用 javascript 设置边距并根据 window 宽度更改 - Set margin with javascript and change it according to window width CSS / Javascript:根据窗口宽度应用javascript - CSS/Javascript: Apply javascript according to window width 使用JavaScript更改浏览器窗口大小时如何更改div宽度 - How to Change a div width when size of browser window change with javascript jQuery根据浏览器宽度进行更改 - Jquery To change according to browser width 根据其他元素的宽度更改框架的宽度 - Change the width of a frame according to the width of the other elements Javascript:window.open:根据页面/正文内容增加高度和宽度 - Javascript : window.open : Increase height+width according to the page/body content 打开不带地址栏和标题的javascript弹出窗口,标题和高度和宽度根据屏幕分辨率按百分比设置 - Opening javascript popup window without address bar and title with height and width set in percentage according to screen resolution javascript如何使里程表在调整窗口大小时更改其宽度 - javascript How to make odometer change its width on window resize 根据屏幕宽度的Javascript Div的宽度 - Javascript Div's Width According to Screen Width 如何基于窗口宽度使用JavaScript更改图像元素类? - How to change image element class with JavaScript based on window width?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM