免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
A Simple jQuery Slideshow
In the interest of following jQuery’s motto of “writing less and doing more,” let’s write a simple slideshow using jQuery, JavaScript and a bit of CSS.

For starters, our main goal should be keeping the markup as clean as possible:

<div id="slideshow"><img src="img/img1.jpg" alt="" class="active" /><img src="img/img2.jpg" alt="" /><img src="img/img3.jpg" alt="" /></div>

Now let’s use CSS to position the images on top of each other and bring the active image to the top level with z-index:

#slideshow {position:relative;height:350px;}#slideshow IMG {position:absolute;top:0;left:0;z-index:8;}#slideshow IMG.active {z-index:10;}#slideshow IMG.last-active {z-index:9;}

Due to absolute positioning, we need to define the height of the slideshow DIV. Also, notice that we defined three different z-indexes—we will manipulate these soon using jQuery.

For the slideshow animation we are going to switch between each photo at a set rate. So let’s start by writing a function that brings in a new photo on top of the last active image:

function slideSwitch() {var $active = $('#slideshow IMG.active');var $next = $active.next();$next.addClass('active');$active.removeClass('active');}$(function() {setInterval( "slideSwitch()", 5000 );});

Here we set a JavaScript interval to call slideSwitch() every 5 seconds. Then slideSwitch() applies the CSS class ‘active’ to bring the next image to the top of the stack. Since we will select the images more than once within slideSwitch(), we define the variables $active and $next forselector performance.

Next we should incorporate a fade animation. For a gallery like this, fade in and fade out are identical, but let’s not forget to think about what we fade against:

function slideSwitch() {var $active = $('#slideshow IMG.active');var $next = $active.next();$active.addClass('last-active');$next.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 1000, function() {$active.removeClass('active last-active');});}$(function() {setInterval( "slideSwitch()", 5000 );});

We start by applying the ‘last-active’ class we defined earlier. Since ‘.last-active’ falls after ‘.active’ in the stylesheet, the z-index of 9 takes priority, and the top image drops back a level. Next, we set the opacity of the new image to 0 so that we can fade it in using the animate() function. Finally, we attach a callback to remove the z-index classes from the previous image when animate() completes.

Although our slideshow is working well, we should make it more robust by building in some default variables. First, let’s define a default active image, in case we need to put less stress on the back-end. Also, we can use defaults to make the gallery animation loop.

function slideSwitch() {var $active = $('#slideshow IMG.active');if ( $active.length == 0 ) $active = $('#slideshow IMG:last');var $next =  $active.next().length ? $active.next(): $('#slideshow IMG:first');$active.addClass('last-active');$next.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 1000, function() {$active.removeClass('active last-active');});}$(function() {setInterval( "slideSwitch()", 5000 );});

We first define a default image for the $active variable, which interestingly enough needs to be the last image on the stack. This is because through absolute positioning, the last image appears on top, and we need to start with it if we want to avoid any flicker.

For the loop it is pretty simple: all we have to do is point the $next variable to the first image once it has gotten to the end of the line.

If you want to improve this function, try setting the animation speed with a variable so the main slideshow function can be thrown into the core and left alone. Also, this slideshow is easily converted to support DIV’s instead of IMG’s—try programming a slideshow with more content.

Now for a challenge: the gallery flickers when the images first load, but it can be fixed without touching the JS or markup at all. Bonus points to whoever figures it out and posts a comment.

By handling the opacity values with the stylesheet and not the JS, you make sure that you are both conserving resources and loading the images with these styles already attached. When coding a JavaScript heavy front end, I like to think of the stylesheets as sort of a base state, which I later manipulate thru scripting.

If you’re a cut-and-paster here’s that portion of the CSS:

#slideshow IMG {position:absolute;top:0;left:0;z-index:8;opacity:0.0;}#slideshow IMG.active {z-index:10;opacity:1.0;}
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
寫給設(shè)計人的10個jQuery特效(表格樹插件)
實驗十二:JQuery打造個性網(wǎng)站
閱讀 jQuery 源碼的18個驚喜
利用jquery實現(xiàn)圖片顯隱特效 | 知更鳥
基于jQuery的圖片異步加載和預(yù)加載實例
將一段js腳本提取為一個單獨的文件
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服