First, let me introduce the jquery data() function
The data() function in jQuery is used to append data to selected elements or obtain data from selected elements. The data accessed through the data() function is temporary data. Once the page is refreshed, the previously stored data will no longer exist.
1. The role of jquery data()
The data() method appends data to the selected element, or obtains data from the selected element.
The data accessed through the data() function is temporary data. Once the page is refreshed, the previously stored data will no longer exist.
This function belongs to a jQuery object (instance). If you need to remove the data stored through the data() function, please use the removeData() function.
2. How to use jquery data
1. Get the value of the attached data
$(selector).data(name)
Parameter description
name:
Optional. Specifies the name of the data to be retrieved.
If no name is specified, this method will return all stored data from the element as an object.
2. Use name and value to append data to the object
$(selector).data(name,value)
Parameter description
selector: an object that needs to append or obtain data.
name: The parameter is the name of the data.
value: The parameter is the value of the data.
3. Use objects to append data to elements
Add data to the selected element using an object with name/value pairs.
In addition to assigning values ??by providing name and value, we can also directly pass in another object ("another") as a parameter. In this case, the attribute name and attribute value of "another" will be treated as multiple key-value pairs, and the "name" and "value" extracted from them will be copied to the cache of the target object.
$(selector).data(object)
Parameter description
object: required. Specifies an object containing name/value pairs.
Example
<html>
<head>
<script type="text/javascript">
$(document).ready(function(){
testObj=new Object();
testObj.greetingMorn="Good Morning!";
testObj.greetingEve="Good Evening!";
$("#btn1").click(function(){
$("div").data(testObj);
});
$("#btn2").click(function(){
alert($("div").data("greetingEve"));
});
});
</script>
</head>
<body>
<button id="btn1">把數(shù)據(jù)添加到 div 元素</button><br />
<button id="btn2">獲取已添加到 div 元素的數(shù)據(jù)</button>
<div></div>
</body>
</html>
Then I will introduce to you the jquery stop() function
The stop() function in jQuery is used to stop the animation running on the currently matched element. By default, the stop() function only stops the currently running animation. If you use the animate() function to set three animations A, B, and C for the current element, if the currently executing animation is A, it will only stop the execution of animation A, and will not prevent the execution of animations B and C. Of course, you can also stop all animations by specifying optional options arguments.
The stop() function in jQuery is used to stop the animation running on the currently matched element.
Stopping the animation does not restore the state before the animation was executed, but stops it directly. The animation will stay in whatever state it is currently in.
For example: perform a transition animation of an element height from 100px to 200px, and stop the animation when the height is 150px, then the current height will still remain at 150px. If the animation sets a callback function after execution, the callback function will not be executed.
1. jquery stop() syntax
$(selector).stop(stopAll,goToEnd)
Parameter description
1. stopAll
Optional. Represents whether to clear the unfinished animation queue.
This means that if the parameter value is true, all subsequent animations or events will be stopped. If the parameter value is false, only the animation currently executed by the selected element will be stopped, and subsequent animations will not be affected. Therefore, this parameter is generally false.
If you use the stop() method, the currently running animation will be stopped immediately. If there is another animation waiting to be executed, the next animation will start with the current state.
2. goToEnd
Optional. Indicates whether to jump the currently executing animation directly to the end of the current animation.
Specifies whether to allow the current animation to be completed. This parameter can only be used when the stopAll parameter is set
3. Remarks
By default, if no parameters are written, both parameters will be considered false.
2. jquery stop() example
HTML code example
<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>jquery stop()</title>
<script type="text/javascript">
$(function(){
$("button:eq(0)").click(function(){
$("#panel").animate({height:"150" }, 1000).animate({width:"300" },
1000).hide(2000).animate({height:"show", width:"show", opacity:"show" }, 1000).animate({height:"500"},
1000);
});
//stop([clearQueue][,gotoEnd]);
//語法結(jié)構(gòu)
$("button:eq(1)").click(function(){
$("#panel").stop();//停止當(dāng)前動(dòng)畫,繼續(xù)下一個(gè)動(dòng)畫
});
$("button:eq(2)").click(function(){
$("#panel").stop(true);//清除元素的所有動(dòng)畫
});
$("button:eq(3)").click(function(){
$("#panel").stop(false, true);//讓當(dāng)前動(dòng)畫直接到達(dá)末狀態(tài) ,繼續(xù)下一個(gè)動(dòng)畫
});
$("button:eq(4)").click(function(){
$("#panel").stop(true, true);//清除元素的所有動(dòng)畫,讓當(dāng)前動(dòng)畫直接到達(dá)末狀態(tài)
});
})
</script>
</head>
<body>
<button>開始一連串動(dòng)畫</button>
<button>stop()</button>
<button>stop(true)</button>
<button>stop(false,true)</button>
<button>stop(true,true)</button>
<div id="panel">
<h5 class="head">什么是jQuery?</h5>
<div class="content">
jQuery。
</div>
</div>
</body>
</html>
實(shí)例說明
1、點(diǎn)擊按鈕(stop()),由于兩個(gè)參數(shù)都是false。所以點(diǎn)擊發(fā)生時(shí),animater沒有跳到當(dāng)前動(dòng)畫(動(dòng)畫1)的最終效果,而直接進(jìn)入動(dòng)畫2,然后動(dòng)畫3,4,5.直至完成整個(gè)動(dòng)畫。
2、點(diǎn)擊按鈕(stop(true)),由于第一個(gè)是true,第二個(gè)是false,所以animater立刻全部停止了,動(dòng)畫不動(dòng)了。
3、點(diǎn)擊按鈕(stop(false,true)),由于第一個(gè)是false,第二個(gè)是true,所以點(diǎn)擊發(fā)生時(shí),animater身處的當(dāng)前動(dòng)畫(動(dòng)畫1)停止并且animater直接跳到當(dāng)前動(dòng)畫(動(dòng)畫1)的最終末尾效果位置,接著正常執(zhí)行下面的動(dòng)畫(動(dòng)畫2,3,4,5),直至完成整個(gè)動(dòng)畫。
3、點(diǎn)擊按鈕(stop(true,true)),由于兩個(gè)都是true,所以點(diǎn)擊發(fā)生時(shí),animater跳到當(dāng)前動(dòng)畫(動(dòng)畫1)的最終末尾效果位置,然后,全部動(dòng)畫停止。
三、jquery stop()在工作中的應(yīng)用
一個(gè)下拉菜單,當(dāng)鼠標(biāo)移上去的時(shí)候就菜單顯示,當(dāng)鼠標(biāo)離開的時(shí)候菜單隱藏 ,如果我快速不斷地將鼠標(biāo)移入移出菜單(即,當(dāng)菜單下拉動(dòng)畫未完成時(shí),鼠標(biāo)又移出了菜單)就會(huì)產(chǎn)生“動(dòng)畫積累",當(dāng)鼠標(biāo)停止移動(dòng)后,積累的動(dòng)畫還會(huì)持續(xù)執(zhí)行,直到動(dòng)畫序列執(zhí)行完畢。
解決方法
在寫動(dòng)畫效果的代碼前加入stop(true,true),這樣每次快速的移入移出菜單,就正常了,當(dāng)移入一個(gè)菜單的時(shí)候,停止所有加入隊(duì)列的動(dòng)畫,完成當(dāng)前的動(dòng)畫(跳至當(dāng)前動(dòng)畫的最終效果位置)。
最后給大家介紹jquery delay()
jquery中delay()方法的功能是設(shè)置一個(gè)延時(shí)值來推遲動(dòng)畫效果的執(zhí)行,它的調(diào)用格式為:$(selector).delay(duration)其中參數(shù)duration為延時(shí)值,它的單位是毫秒,當(dāng)超過延時(shí)值時(shí),動(dòng)畫繼續(xù)執(zhí) ,delay與setTimeout函數(shù)還是有區(qū)別的,delay是更適合某些使用情況。
可以將隊(duì)列中等待執(zhí)行的下一個(gè)動(dòng)畫延遲指定的時(shí)間后才執(zhí)行。它常用在隊(duì)列中的兩個(gè)jQuery效果函數(shù)之間,從而在上一個(gè)動(dòng)畫效果執(zhí)行后延遲下一個(gè)動(dòng)畫效果的執(zhí)行時(shí)間。
一、語法
$(selector).delay(speed,queueName)
1、參數(shù)說明
2、備注
延時(shí)時(shí)間(duration參數(shù))是以毫秒為單位的,數(shù)值越大,動(dòng)畫越慢,不是越快。
字符串 'fast' 和 'slow' 分別代表200和600毫秒的延時(shí)。
二、delay()實(shí)例
HTML
<p>動(dòng)畫效果:
<select id="animation">
<option value="1">動(dòng)畫1</option>
<option value="2">動(dòng)畫2</option>
<option value="3">動(dòng)畫3</option>
<option value="4">動(dòng)畫4</option>
</select>
<input id="exec" type="button" value="執(zhí)行動(dòng)畫" >
</p>
<div id="myDiv" style="width:300px; height: 100px; background-color: #eee;">CodePlayer</div>
<script>
$("#exec").click( function(){
var v = $("#animation").val();
var $myDiv = $("#myDiv");
if(v == "1"){
$myDiv.slideUp( 1000 )
.delay( "slow" )
.fadeIn( 1500 );
}else if(v == "2"){
$myDiv.fadeOut( "slow" )
.delay( 2000 )
.slideDown( 1000 )
.animate( { height: "+=300" } );
}else if(v == "3"){
/*
注意:只有動(dòng)畫才會(huì)被加入效果隊(duì)列中
以下代碼實(shí)際上只有slideUp()、slideDown()會(huì)加入效果隊(duì)列
delay()的延遲只對(duì)slideDown()起作用
show()在被調(diào)用時(shí)就立即執(zhí)行了(此時(shí)slideUp的動(dòng)畫效果尚未執(zhí)行結(jié)束)
以下代碼的執(zhí)行順序時(shí):
1、slideUp()被加入隊(duì)列、開始執(zhí)行,
2、show()也開始執(zhí)行,它立即執(zhí)行完畢,此時(shí)slideUp()的動(dòng)畫尚未執(zhí)行結(jié)束
3、延遲2秒
4、執(zhí)行SlideDown()
*/
$myDiv.slideUp( "slow" )
.delay( 2000 )
.show( ) // 它不是一個(gè)效果動(dòng)畫
.slideDown( );
}else if(v == "4"){
$myDiv.show()
.delay( 2000 )
// 在現(xiàn)有高度的基礎(chǔ)上增加300px (如果原來是100px,增加后就是400px)
.animate( { height: "+=300px" }, 2000 )
.animate( { width: "50%" }, 1000 )
.animate( { width: "200px", height: "100px" }, 1000 );
}
} );
</script>
實(shí)例二、讓頁面中的按鈕在頁面加載后500毫秒隱藏,然后再過1500毫秒顯示出來
$(function(){
var $inputs = $('input[type=button]')
.delay(500)
.queue(function(){$(this).hide().dequeue();})
.delay(1500)
.queue(function(){$(this).show();});
});
三、jquery中使用delay()注意事項(xiàng)
1、delay適用在jQuery動(dòng)畫效果和類似隊(duì)列中
2、如果下一項(xiàng)是動(dòng)畫效果,則會(huì)執(zhí)行延遲調(diào)用
3、如果不是效果動(dòng)畫,則它不會(huì)被加入效果隊(duì)列中,因此該函數(shù)不會(huì)對(duì)它進(jìn)行延遲調(diào)用。
4、如果要將不是動(dòng)畫效果加入延遲,則需要將它加入到queue隊(duì)列中去。
例如
$(function(){
var $inputs = $('input[type=button]')
.delay(500)
.queue(function(){$(this).hide();})
.delay(1500)
.show(1);
//.queue(function(){$(this).show();});
});
備注:上面方法只隱藏,不會(huì)再顯示,queue執(zhí)行完后,也中止了動(dòng)畫隊(duì)列的繼續(xù)執(zhí)行,需要調(diào)用dequeue使其執(zhí)行下去
又如
$(function(){
var $inputs = $('input[type=button]')
.delay(500)
.queue(function(){$(this).hide().dequeue();})
.delay(1500)
.show();
//.show(1);
});
備注:上面方法也是只隱藏,而不會(huì)再顯示!這里show不再指定顯示動(dòng)畫時(shí)長(zhǎng),則show方法不再是一個(gè)動(dòng)畫。由此可知,dequeue只能使得動(dòng)畫隊(duì)列中的后續(xù)方法執(zhí)行下去,不能使非動(dòng)畫隊(duì)列中的jquery方法繼續(xù)執(zhí)行!