JQuery 课时23 jQuery css属性
1、.css()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="./jquery-3.6.0.js"></script>
</head>
<body>
<div>div</div>
<script>
$(function(){
$("div").css("color","red");
// 获取某个属性值
console.log($("div").css("color"));
})
</script>
</body>
</html>
2、.height() 获取当前高度值或者设置高度值,不受margin、padding等影响;能接受参数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="./jquery-3.6.0.js"></script>
<style>
div{
width: 100px;
height: 100px;
padding: 20px;
margin: 30px;
border: 2px solid red;
}
</style>
</head>
<body>
<div>div</div>
<script>
$(function(){
console.log($("div").height());
})
</script>
</body>
</html>
3、 .width()获取当前宽度值或者设置宽度值
width跟height()一样用法;能接受参数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="./jquery-3.6.0.js"></script>
<style>
div{
width: 100px;
height: 100px;
padding: 20px;
margin: 30px;
border: 2px solid red;
}
</style>
</head>
<body>
<div>div</div>
<script>
$(function(){
$("div").height(50);
$("div").width(50);
})
</script>
</body>
</html>
设置height、width
4、innerHeight()
获取匹配元素的第一个当前计算高度, 包含padding,但不包含border;不能接受参数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="./jquery-3.6.0.js"></script>
<style>
div{
width: 100px;
height: 100px;
padding: 20px;
margin: 30px;
border: 2px solid red;
}
</style>
</head>
<body>
<div>div</div>
<script>
$(function(){
console.log($("div").innerHeight());
console.log($("div").innerWidth());
})
</script>
</body>
</html>
包含了上下的padding
5、innerWidth()
获取匹配元素的第一个当前计算宽度, 包含padding,但不包含border;不能接受参数
6、 .offset()
获取匹配元素的第一个元素的当前坐标,或设置匹配元素的每个元素的坐标,坐标相对于文档
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="./jquery-3.6.0.js"></script>
<style>
div{
width: 100px;
height: 100px;
padding: 20px;
margin: 30px;
border: 2px solid red;
}
</style>
</head>
<body>
<div>div</div>
<script>
$(function(){
var div = $("div");
// 获得相对于文档的坐标
console.log(div.offset());
// 设置坐标
var zuobiao={
top:300,
left:500
};
console.log(div.offset(zuobiao));
})
</script>
</body>
</html>
7、outerHeight()
8、outerWidth()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="./jquery-3.6.0.js"></script>
<style>
div{
width: 100px;
height: 100px;
padding: 20px;
margin: 30px;
border: 2px solid red;
}
</style>
</head>
<body>
<div>div</div>
<script>
$(function(){
var div = $("div");
console.log(div.outerHeight());
console.log(div.outerWidth());
})
</script>
</body>
</html>
加上参数为true,计算margin
<script>
$(function(){
var div = $("div");
console.log(div.outerHeight());
console.log(div.outerWidth(true));//加上margin
})
</script>
9、position()-相对于 父元素
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="./jquery-3.6.0.js"></script>
<style>
*{
margin: 0;
padding: 0;
}
div{
padding: 15px;
background-color: antiquewhite;
position: absolute;
}
p{
margin-left:10px;
background-color: aqua;
}
</style>
</head>
<body>
<div>
<p>我是p</p>
</div>
<script>
$(function(){
var po=$("p").position();
console.log(po);
})
</script>
</body>
</html>
10、scrollLeft()
11、scrollTop()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="./jquery-3.6.0.js"></script>
<style>
.demo{
width: 200px;
height: 100px;
overflow: auto;
margin: 5px;
padding: 5px;
position: relative;
border: 3px solid #666666;
background-color: #cccccc;
}
p{
width: 1000px;
height: 1000px;
margin: 10px;
padding: 5px;
border: 2px solid #666666;
}
</style>
</head>
<body>
<div class="demo">
<h1>lalal</h1>
<p>hellp</p>
</div>
<script>
$(function(){
// 设置滚动300
$(".demo").scrollTop(300);
//获取滚动值
console.log($(".demo").scrollTop());
})
</script>
</body>
</html>