JQuery:改变body的CSS属性留在窗口调整大小 [英] JQuery: Change body css attribute left on window resize

查看:1042
本文介绍了JQuery:改变body的CSS属性留在窗口调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



JQuery:


$ b $

  $(window).resize(function(){
var w = $(body)。 810< 0){
$(body)。css(Left,0px);
}
else {
var width = w.toString )+px;
$(body)css(Left,width);
}
}

CSS:

 code> body {
background-color:#eeeeee;
position:relative;
left:20%;
}

HTML:

 < script src =scripts\jquery.min.jstype =text / javascript>< / script& 
< script src =scripts\myJavascript.jstype =text / javascript>< / script>
....
< body class =body>

我注意到了一件事,如果我通过类选择body(将jquery改为#body

解决方案



如果你想通过类获得body元素,你必须使用'。body'选择器。如果你想获取带有id的元素,使用'#body'



尝试将CS​​S放在小写,更好地传递一个对象作为参数。此外,还尝试缓存body元素,以防止对DOM树进行多个不必要的查询:

  var body = $('。body'); 

$(window).resize(function(){
var w = body.width();

if(w-810< 0){
w =0;
}

body.css({left:w +px});
}


I'm trying to get my website to scale its width when the window size is changed.

JQuery:

$(window).resize(function() {
    var w = $("body").width();
    if(w-810 < 0) {
        $("body").css("Left","0px");
    }
    else {
        var width = w.toString() + "px";
        $("body").css("Left",width); 
    }
});

CSS:

body {
background-color: #eeeeee;
position: relative;
left: 20%;
}

HTML:

<script src="scripts\jquery.min.js" type="text/javascript"></script>
<script src="scripts\myJavascript.js" type="text/javascript"></script>
....
<body class="body">

One thing I have noticed, if I select the body by class (change in jquery to "#body"), it always defaults to 0px for the width.

Any ideas?

解决方案

If you want to get the body element by a class, you must use the '.body' selector. If you want to get elements with their id, use '#body'

Try to put the CSS in lowercase, better passing in an object as parameter. Moreover, try also to "cache" the body element in order to prevent several, unnecessary querys to the DOM tree:

var body = $('.body');

$(window).resize(function() {
    var w = body.width();

    if (w-810 < 0) {
        w = "0";
    }

    body.css({ "left" : w + "px" });
});

这篇关于JQuery:改变body的CSS属性留在窗口调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆