欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jQuery操作css方法的超全用法

 更新時間:2023年06月09日 15:01:18   作者:LazyKing-qi  
最近看了下jQuery的源代碼,其中關(guān)于CSS及className的操作思想確實(shí)很不錯,值得借鑒,下面這篇文章主要給大家介紹了關(guān)于jQuery操作css方法的超全用法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下

一、jQuery可以使用css方法來修改元素樣式

jQuery可以使用css方法來修改簡單元素樣式;也可以操作類,修改多個樣式。

1.參數(shù)只寫屬性名,則是返回屬性值

$(this).css("color"");

⒉.參數(shù)是屬性名,屬性值,逗號分隔,是設(shè)置一組樣式,屬性必須加號,值如果是數(shù)字可以不用

跟單位和引號

$(this).css("color", "red");

 示例:

<body>
		<div>123</div>
		<script>
			$(function(){
				$("div").css("color","red");
			});
		</script>
</body>

3.參數(shù)可以是對象形式,方便設(shè)置多組樣式。屬性名和屬性值用冒號隔開,屬性可以不用加引號,

$(this).css({ "color":" white","font-size":"20px"});

示例:

<body>
		<div></div>
		<script>
			$(function(){
				$("div").css({
					width:200,
					height:200,
					backgroundColor:"red"
				});
			});
		</script>
</body>

二、設(shè)置類的樣式方法

作用等同于以前的classList,可以操作類樣式,注意操作類里面的參數(shù)不要加點(diǎn)。

1.添加類

$("div").addClass("current");

2.移除類

$("div").removeClass("current");

3.切換類

$("div").toggleClass("current");

示例:

<style>
			div{
				width: 100px;
				height: 100px;
				background-color: aquamarine;
				margin: 100px auto;
				transition: all 0.5s;
			}
			.current{
				background-color: red;
				transform: rotate(360deg);
			}
		</style>
	</head>
	<body>
		<div class="current"></div>
		<script>
			//添加類addClass()
			/* $(function(){
				$("div").click(function(){
					$(this).addClass("current");
				});
			}) */
			//刪除類removeClass()
			/* $("div").click(function(){
				$(this).removeClass("current");
			}); */
			//切換類 toggleClass()
			$("div").click(function(){
				$(this).toggleClass("current");
			});
		</script>
	</body>

三、類操作與className區(qū)別

原生JS中className會覆蓋元素原先里面的類名。

jQuery里面類操作只是對指定類進(jìn)行操作,不影響原先的類名。

<body>
		<div class="one"></div>
		<script>
			//追加類名,覆蓋原來的類名
			var one = document.querySelector(".one");
			one.className = "two";
            //addClass相當(dāng)于追加類名,不影響以前的類名
			$(".one").addClass("two");
			//移除two這個類名
			//$(".one").removeClass("two");
		</script>
</body>

四、顯示隱藏效果

1、顯示語法

show ([speed,[easing],[fn]])

顯示參數(shù)

(1)參數(shù)都可以省略,無動畫直接顯示。

( 2 ) speed :三種預(yù)定速度之一的字符串(“slow”,"norma",or“fast”)或表示動畫時長的毫秒數(shù)值

(如:1000)。

( 3 ) easing : (Optional)用來指定切換效果,默認(rèn)是“swing”,可用參數(shù)“linear”。

( 4 ) fn:回調(diào)函數(shù),在動畫完成時執(zhí)行的函數(shù),每個元素執(zhí)行一次。

2、隱藏語法

hide([speed,[easing],[fn]])

3、切換語法

toggle([speed,[easing],[fn]])

 示例:

<style>
			div{
				width: 100px;
				height: 100px;
				background-color: aquamarine;
			}
		</style>
	</head>
	<body>
		<button>顯示</button>
		<button>隱藏</button>
		<button>切換</button>
		<div></div>
		<script>
			$(function(){
				$("button").eq(0).click(function(){
					$("div").show(1000,function(){
						alert(1);
					});
				});
				$("button").eq(1).click(function(){
					$("div").hide(1000,function(){
						alert(1);
					});
				});
				//一般情況不加參數(shù),直接顯示隱藏
				$("button").eq(2).click(function(){
					$("div").toggle(1000);
				});
			})
		</script>
	</body>

五、滑動效果

1、上滑效果語法

slideUp ([speed,[easing],[fn]])

2、下滑效果語法

slideDown([speed,[easing],[fn]])

3、滑動切換效果語法

slideToggle ([speed,[easing],[fn]])

 示例:

<style>
			div{
				width: 100px;
				height: 100px;
				background-color: aquamarine;
				display: none;
			}
		</style>
	</head>
	<body>
		<button>下滑</button>
		<button>上滑</button>
		<button>滑動切換</button>
		<div></div>
		<script>
			$(function(){
				//下滑slideDown()
				$("button").eq(0).hover(function(){
					$("div").slideDown();
				})
				//上滑slideUp()
				$("button").eq(1).hover(function(){
					$("div").slideUp();
				})
				//滑動切換slideToggle()
				$("button").eq(2).hover(function(){
					$("div").slideToggle();
				})
			})
		</script>
</body>

4、事件切換

hover([over,]out)

( 1 ) over:鼠標(biāo)移到元素上要觸發(fā)的函數(shù)(相當(dāng)于mouseenter )

( 2 ) out:鼠標(biāo)移出元素要觸發(fā)的函數(shù)(相當(dāng)于mouseleave )

六、動畫隊(duì)列及其停止排隊(duì)方法

1、動畫或效果隊(duì)列

動畫或者效果一旦觸發(fā)就會執(zhí)行,如果多次觸發(fā),就造成多個動畫或者效果排隊(duì)執(zhí)行。

2、停止排隊(duì)

stop()

(1 ) stop()方法用于停止動畫或效果。

(2)注意:stop()寫到動畫或者效果的前面,相當(dāng)于停止結(jié)束上一次的動畫。

<script>
	$(function(){
	//下滑slideDown()
	$("button").eq(0).hover(function(){
		$("div").stop().slideDown();
		})
		//上滑slideUp()
		$("button").eq(1).hover(function(){
			$("div").stop().slideUp();
		})
		//滑動切換slideToggle()
		$("button").eq(2).hover(function(){
		$("div").stop().slideToggle();
	    });
	});
</script>

七、淡入淡出效果

1、淡入效果語法

fadeIn([speed,[easing],[fn]])

淡入效果參數(shù)

(1)參數(shù)都可以省略。

( 2 ) speed :三種預(yù)定速度之一的字符串(“slow”,“normal”,or"fast”)或表示動畫時長的毫秒數(shù)值(如

∶1000)。

( 3 ) easing : (Optional)用來指定切換效果,默認(rèn)是“swing”,可用參數(shù)“linear”。.

( 4 ) fn:回調(diào)函數(shù),在動畫完成時執(zhí)行的函數(shù),每個元素執(zhí)行一次。

2、淡出效果語法

fadeout([speed,[easing],[fn]])

3、淡入淡出切換效果語法

fadeToggle([speed, [easing],[fn]])

4、漸進(jìn)方式調(diào)整到指定的不透明度

fadeTo ([[speed],opacity,[easing],[fn]])

2.效果參數(shù)

( 1 ) opacity透明度必須寫,取值0~1之間。

( 2 ) speed :三種預(yù)定速度之一的字符串(“slow”,"normal" ,or“fast”)或表示動畫時長的毫秒數(shù)值(如∶1000)。必須寫

( 3 ) easing : (Optional)用來指定切換效果,默認(rèn)是“swing”,可用參數(shù)“linear”。

( 4 ) fn:回調(diào)函數(shù),在動畫完成時執(zhí)行的函數(shù),每個元素執(zhí)行一次。

示例:

<style>
			div{
				width: 100px;
				height: 100px;
				background-color: aquamarine;
				display: none;
			}
		</style>
	</head>
	<body>
		<button>淡入</button>
		<button>淡出</button>
		<button>淡入淡出</button>
		<button>修改不透明度</button>
		<div></div>
		<script>
			$(function(){
				$("button").eq(0).click(function(){
					$("div").fadeIn(1000);
				})
				$("button").eq(1).click(function(){
					$("div").fadeOut(1000);
				})
				$("button").eq(2).click(function(){
					$("div").fadeToggle(1000);
				})
				//透明度必須要寫
				$("button").eq(3).click(function(){
					$("div").fadeTo(1000,0.5);
				});
			});
		</script>
</body>

八、自定義動畫animate

語法:

animate(params, [speed],[easing],[fn])

參數(shù)

( 1 ) params:想要更改的樣式屬性,要以對象的形式傳遞,必須寫。屬性名可以不用帶引號,如果

是復(fù)合屬性則需要采取駝峰命名法borderLeft。其余參數(shù)都可以省略。

( 2 ) speed :三種預(yù)定速度之一的字符串(“slow”,"normal”,or“fast”)或表示動畫時長的毫秒數(shù)值(如

∶1000)。

( 3 ) easing : (Optional)用來指定切換效果,默認(rèn)是“swing”,可用參數(shù)“linear”。

( 4 ) fn:回調(diào)函數(shù),在動畫完成時執(zhí)行的函數(shù),每個元素執(zhí)行一次。

示例:

<style>
			div{
				//盒子定位
				position: absolute;
				width: 100px;
				height: 100px;
				background-color: aquamarine;
			}
		</style>
	</head>
	<body>
		<button>動起來</button>
		<div></div>
		<script>
			$(function(){
				$("button").click(function(){
					$("div").animate({
						left: 300,
						top: 200,
						opacity: 0.4,
						width: 200,
						height: 200
					},500);
				});
			});
		</script>
	</body>

總結(jié)

到此這篇關(guān)于jQuery操作css方法的文章就介紹到這了,更多相關(guān)jQuery操作css方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論