Chapter 6 JQuery operating style
6.1 CSS operation
-
Function: Set or modify the style, the style attribute operation.
-
Operating a single style
// name:需要设置的样式名称
// value:对应的样式值
// $obj.css(name, value);
// 使用案例
$('#one').css('background','gray');// 将背景色修改为灰色
- Setting multiple styles
// 参数是一个对象,对象中包含了需要设置的样式名和样式值
// $obj.css(obj);
// 使用案例
$('#one').css({
'background':'gray',
'width':'400px',
'height':'200px'
});
- Gets the style
// name:需要获取的样式名称
// $obj.css(name);
// 案例
$('div').css('background-color');
Note: Gets the style operation only returns style values corresponding to the first element.
6.2 jQuery size and position of the operation
Methods 6.2.1 width and height methods
- Get or set the height, not including padding, borders and margins
// 带参数表示设置高度
$('img').height(200);
// 不带参数获取高度
$('img').height();
Get the visible region of the width and height of the page
// 获取可视区宽度
$(window).width();
// 获取可视区高度
$(window).height();
6.2.2 innerWidth/innerHeight/outerWidth/outerHeight
innerWidth()/innerHeight() 方法返回元素的宽度/高度(包括内边距)。
outerWidth()/outerHeight() 方法返回元素的宽度/高度(包括内边距和边框)。
outerWidth(true)/outerHeight(true) 方法返回元素的宽度/高度(包括内边距、边框和外边距)。
6.2.3 scrollTop与scrollLeft
- Get or set the position of the vertical scroll bar
// 获取页面被卷曲的高度
$(window).scrollTop();
// 获取页面被卷曲的宽度
$(window).scrollLeft();
6.2.4 offset method and position method
- The method of obtaining the position offset of the element from the document, position obtaining method from the position of the positioning element is the parent element (the offsetParent) a.
// 获取元素距离document的位置,返回值为对象:{left:100, top:100}
$(selector).offset();
// 获取相对于其最近的有定位的父元素的位置。
$(selector).position();