在jquery中,可以利用css方法來修改元素的定位屬性,該方法用于設(shè)置或者返回指定元素的一個或者多個樣式屬性;只需將該方法的第一個參數(shù)設(shè)置為“position”定位屬性,第二個參數(shù)設(shè)置為更改后的定位屬性即可,語法為“元素對象.css("position","更改后的定位屬性值");”。
本教程操作環(huán)境:windows10系統(tǒng)、jquery3.4.1版本、Dell G3電腦。
jquery怎么改css定位屬性
css() 方法設(shè)置或返回被選元素的一個或多個樣式屬性。
設(shè)置 CSS 屬性
如需設(shè)置指定的 CSS 屬性,請使用如下語法:
css("propertyname","value");
如需設(shè)置多個 CSS 屬性,請使用如下語法:
css({"propertyname":"value","propertyname":"value",...});
position 屬性指定了元素的定位類型。
position 屬性的五個值:
-
static
-
relative
-
fixed
-
absolute
-
sticky
元素可以使用的頂部,底部,左側(cè)和右側(cè)屬性定位。然而,這些屬性無法工作,除非是先設(shè)定position屬性。他們也有不同的工作方式,這取決于定位方法。
示例如下:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="js/jquery.min.js"> </script> <script> $(document).ready(function(){ $("button").click(function(){ $(".pos_fixed").css("position","static"); }); }); </script> <style> p.pos_fixed { position:fixed; top:30px; right:5px; } </style> </head> <button>返回元素的定位屬性</button> <body> <p class="pos_fixed">Some more text</p> <p><b>注意:</b> IE7 和 IE8 支持只有一個 !DOCTYPE 指定固定值.</p> <p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p><p>Some text</p> </body> </html>
輸出結(jié)果:
視頻教程推薦:jQuery視頻教程