1、元素的顯示和隱藏
簡單顯示和隱藏方法
<script type="text/javascript"> function f1(){ //隱藏 $("div").hide();//display:none //document.getElementById('id').style.display="none"; } function f2(){ //顯示 $("div").show();//display:block } function f3(){ $("div").toggle(); }</script><style type="text/css"> div {width:300px; height:200px; background-color:blue;}</style> <body> <div>duck and dog</div> <input type="button" value="隱藏" onclick="f1()" /> <input type="button" value="顯示" onclick="f2()" /> <input type="button" value="開關效果" onclick="f3()" /></body>CSS支持兩種方法顯示和隱藏元素,即使用visibility或display樣式,他們控制元素顯示和隱藏的時候效果相同,但是結果卻不同。
具體說明如下:
2、滑動效果顯示和隱藏
speed:設置效果的速度(slow(600)normal(400) fast(200) 時間(毫秒))
callback: 效果執行完畢之后自動調用的回調函數
<script type="text/javascript"> function f1(){ //隱藏 $("div").slideUp(3000,function(){ alert('我消失了,你能看到么'); }); } function f2(){ //顯示 $("div").slideDown(3000,function(){ alert('我又回來了'); });//display:block } function f3(){ $("div").slideToggle(1000); } $(function(){ //氣缸滑動效果 //setInterval("f3()",1000); });</script><style type="text/css">div {width:300px; height:200px; background-color:blue;}</style><body> <div>duck and dog</div> <input type="button" value="隱藏" onclick="f1()" /> <input type="button" value="顯示" onclick="f2()" /> <input type="button" value="開關效果" onclick="f3()" /> </body>3、淡入淡出效果
讓元素通過一定透明度變化,達到顯示和隱藏的效果
<script type="text/javascript"> function f1(){ //隱藏 $("div").fadeOut(4000); } function f2(){ //顯示 $("div").fadeIn(4000); $("#two").fadeTo(2000,0.3); } function f3(){ $("div").fadeToggle(2000); }</script><style type="text/css"> div {width:300px; height:200px; background-color:blue;}</style><body> <div id = "two">duck and dog</div> <input type="button" value="隱藏" onclick="f1()" /> <input type="button" value="顯示" onclick="f2()" /> <input type="button" value="開關效果" onclick="f3()" /></body>設置透明度,div的透明度是30%:
4、動畫效果底層方法animate()
show() hide() 等等動畫效果內部都是執行animate()方法
$().animate(css效果參數[,時間][,回調函數]);
css()等一般jquery方法執行完畢之后會返回當前jquery對象,因此jquery的許多方法可以鏈式調用。
<script type="text/javascript"> function f1(){ //文字大小、文字粗體、div本身寬度和高度 //font-size background-color color console.log($("div")); //jquery對象調用完畢css方法本身還是一個jquery對象 //說明css方法執行完畢有return this關鍵字 console.log($("div").css('font-weight','bold').css("background-color",'pink')); var jn = {'font-size':'30px',width:'400px',height:'300px'}; $("div").css('font-weight',"bold").css("background-color","pink").css("color","white").animate(jn,2000); //$("div").animate(jn,2000); }</script><style type="text/css"> div {width:300px; height:200px; background-color:blue; }</style><body> <div>duck and dog</div> <input type="button" value="設置" onclick="f1()" /></body>5、累加累減動畫
如果動畫一次設定left:500 ,第一次單擊div會左移500像素,第二次單擊就不會動了。累加累減就是連續動的,只需要將left:”500px”改成left:”+=500px”或者left:”-=500px”即可。
(function(){ $("#panel").click(function(){ $(this).animate({left: "+=500px"}, 3000); }) })</span> 6、多重動畫
1、同時執行多個動畫
上面的例子只控制了left屬性的變化,這回我們在控制left屬性的時候同時控制元素高度變為200px
$(function(){ $("#panel").click(function(){ $(this).animate({left: "500px",height:"200px"}, 3000); }) })2、順序執行動畫
上面例子中元素右移和放大高度兩個動畫是同時進行的。現在我們要實現先右移再放大高度,那很簡單,只需要把上面的animate()方法拆開寫成兩個即可
$(function(){ $("#panel").click(function(){ $(this).animate({left: "500px"},3000) .animate({height:"200px"},1000); }) }) 3、綜合動畫
接下來完成更復雜的動畫。單擊div元素后讓他向右移動的同時增大他的高度,并將它的不透明度從50%切換到100%。然后再讓它從上向下移動,同時它的寬度變大,當完成這
些效果后讓它以淡出的方式隱藏掉。
$(function(){ $("#panel").css("opacity",0.5);//設置不透明度 $("#panel").click(function(){ $(this).animate({left: "400px",height:"200px",opacity:"1"},3000) .animate({top:"200px",width:"200px"},3000) .fadeOut(1000); }) }) 7、動畫回調函數
在上例中,如果想在最后一步切換css樣式而不是隱藏元素。這我們就可以用到animate的第三個參數回調函數了
$(function(){ $("#panel").css("opacity",0.5);//設置不透明度 $("#panel").click(function(){ $(this).animate({left: "400px",height:"200px",opacity:"1"},3000) .animate({top:"200px",width:"200px"},3000,function(){$(this).css("border","5px solid blue")}); }) }) 這樣就把css方法加入到動畫隊列中了。
8、停止動畫和判斷是否處于動畫狀態
1、停止元素的動畫
stop([clearQueue][,gotoEnd]) 兩個都是可選參數,都是boolean類型
參數說明:
clearQueue:代表是否清空未執行完的動畫隊列
gotoEnd :代表是否將正在執行的動畫跳到末狀態
通過一個綜合的示例就可以弄明白stop()方法的這兩個參數了:
<style type="text/css"> *{margin:0;padding:0;} body { font-size: 13px; line-height: 130%; padding: 60px } #panel { width: 60px; border: 1px solid #0050D0 ;height:22px;overflow:hidden;} .head { padding: 5px; background: #96E555; cursor: pointer;width: 300px;} .content { padding: 10px; text-indent: 2em; border-top: 1px solid #0050D0;display:block; width:280px;} </style> <script src="../../../scripts/jquery.js" type="text/javascript"></script> <script type="text/javascript"> $(function(){ $("button:eq(0)").click(function () { $("#panel") .animate({height : "150" } , 2000 ) .animate({width : "300" } , 2000 ) .hide(3000) .animate({height : "show" , width : "show" , opacity : "show" } , 2000 ) .animate({height : "500"} , 2000 ); }); $("button:eq(1)").click(function () { $("#panel").stop();//停止當前動畫,繼續下一個動畫 }); $("button:eq(2)").click(function () { $("#panel").stop(true);//清除元素的所有動畫 }); $("button:eq(3)").click(function () { $("#panel").stop(false,true);//讓當前動畫直接到達末狀態 ,繼續下一個動畫 }); $("button:eq(4)").click(function () { $("#panel").stop(true,true);//清除元素的所有動畫,讓當前動畫直接到達末狀態 }); }) </script> <body> <button>開始一連串動畫</button> <button>stop()</button> <button>stop(true)</button> <button>stop(false,true)</button> <button>stop(true,true)</button> <div id="panel"> <h5 class="head">三國殺殺天下</h5> <div class="content"> 夏侯
主站蜘蛛池模板:
广安市|
方山县|
红原县|
界首市|
北辰区|
睢宁县|
阿克陶县|
古交市|
德令哈市|
星子县|
丰顺县|
宜兰市|
文成县|
平舆县|
安龙县|
桓仁|
永仁县|
新源县|
汽车|
泸州市|
青冈县|
镇巴县|
昌图县|
内黄县|
祁阳县|
富民县|
紫云|
涿州市|
武夷山市|
曲水县|
改则县|
新巴尔虎左旗|
汝阳县|
重庆市|
汤阴县|
德令哈市|
涪陵区|
南投市|
东丽区|
海城市|
禄丰县|