简体   繁体   English

jQuery分辨率更改CSS

[英]jQuery resolution change css

I am bussy with a responive design and i have this js code 我忙于响应式设计,我有这个js代码

$("#gradient #wrapper #camboxs .cambox:nth-child(5n)").not("#gradient #wrapper #footer .box #camboxs .combox").css("margin-right","0");

I want when somewone resize there brower smaller then 980px or the browser starts on 980px or smaller then the function change to: 我想在有人调整大小时将其设置为小于980px,或者浏览器以980px或更小尺寸启动,然后功能更改为:

$("#gradient #wrapper #camboxs .cambox:nth-child(2n)").not("#gradient #wrapper #footer .box #camboxs .combox").css("margin-right","0");

How can i do this? 我怎样才能做到这一点?

Why are you using javascript for this? 您为什么要为此使用javascript? Can be done with pure css: 可以使用纯CSS完成:

#gradient #wrapper #camboxs .cambox:nth-child(5n):not(#gradient #wrapper #footer .box #camboxs .combox)
{
    margin-right: 0;
}

And then use media queries: 然后使用媒体查询:

@media screen and (max-width: 980px), projection and (max-width: 980px)
{
    /* first undo the general styles */
    #gradient #wrapper #camboxs .cambox:nth-child(5n):not(#gradient #wrapper #footer .box #camboxs .combox)
    {
        margin-right: 10px; /* replace with the original margin */
    }
    #gradient #wrapper #camboxs .cambox:nth-child(2n):not(#gradient #wrapper #footer .box #camboxs .combox)
    {
        margin-right: 0;
    }
}

Like Andy suggested, it could be done with way less code. 就像安迪建议的那样,可以用更少的代码来完成。

And for the fallback: To react on window resize, use the following with jQuery (untested): 对于备用:要对窗口调整大小做出反应,请对jQuery使用以下内容(未经测试):

$(window).resize(function() {
    if ($(window).width() <= 980) {
        $("#gradient #wrapper #camboxs .cambox:nth-child(5n)").not("#gradient #wrapper #footer .box #camboxs .combox").css("margin-right","");
        $("#gradient #wrapper #camboxs .cambox:nth-child(2n)").not("#gradient #wrapper #footer .box #camboxs .combox").css("margin-right","0");
    } else {
        $("#gradient #wrapper #camboxs .cambox:nth-child(2n)").not("#gradient #wrapper #footer .box #camboxs .combox").css("margin-right","");
        $("#gradient #wrapper #camboxs .cambox:nth-child(5n)").not("#gradient #wrapper #footer .box #camboxs .combox").css("margin-right","0");
    }
});

I'd strongly advise using media queries and css3 selectors and then using a shim or polyfill. 我强烈建议您使用媒体查询和css3选择器,然后再使用填充程序或polyfill。 We shouldn't sacrifice code quality and performance for the sake of a 10+ year old browser. 我们不应为了使用10年以上的浏览器而牺牲代码质量和性能。

So, here's a media query emulator for IE7/8: 因此,这是IE7 / 8的媒体查询模拟器:

http://ie7-js.googlecode.com/svn/version/2.0%28beta3%29/IE8.js http://ie7-js.googlecode.com/svn/version/2.0%28bet​​a3%29/IE8.js

and here's a css3 polyfill for IE6/7/8: 这是针对IE6 / 7/8的css3 polyfill:

http://selectivizr.com/ http://selectivizr.com/

Have fun! 玩得开心!

@media only screen 
and (min-device-width : 980px)  {
    /* Styles */

    $("#gradient #wrapper #camboxs .cambox:nth-child(2n)").not("#gradient #wrapper #footer .box #camboxs .combox").css("margin-right","0");
}

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

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