jqueryが実現する簡単な輪播図機能【初心者向け】

5831 ワード

本明細書の例は、jqueryによって実現されるタームキャスト機能を説明する。皆さんに参考にしてあげます。具体的には以下の通りです。
前に原生js実現の輪播図を紹介しましたが、今日はjqueryで輪播図を作ってみました。方法は同じですが、jqueryの実現はjsよりずっと便利になりました。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>www.jb51.net jQuery   </title>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<style>
*{
margin: 0px;
padding: 0px;
list-style: none;
}
#flash{
width: 400px;
height: 200px;
position: relative;
margin: 0 auto;
border:1px solid black;
overflow: hidden;
}
#falsh img{
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
}
#flash ul{
position: absolute;
left: 25%;
bottom: 5%;
width: 200px;
height: 30px;
border-radius: 20px;
background-color: rgba(0,0,0,0.5);
}
#flash ul li{
float: left;
width: 15px;
height: 15px;
border-radius: 50%;
background-color: #fff;
margin-left: 20px;
margin-top: 7px;
}
#flash ul .li_first{
background-color: #f40;
}
#flash .button{
width: 50px;
height: 50px;
border-radius: 50%;
background-color: rgba(0,0,0,0.3);
cursor: pointer;
}
#flash .right{
position: absolute;
right: 10px;
top: 80px;
}
#flash .left{
position: absolute;
left: 10px;
top: 80px;
}
#flash .button span{
font-size: xx-large;
font-weight: 700;
line-height: 50px;
margin-left: 15px;
color: rgba(255,255,255,0.5);
}
</style>
</head>
<body>
<div id="flash">
<img src="http://demo.jb51.net/js/2018/html5-css3-3d-img-flash-codes/images/Guardians-of-the-Galaxy-Poster-High-Res.jpg" alt="" style="display: block">
<img src="http://demo.jb51.net/js/2018/html5-css3-3d-img-flash-codes/images/Blade-Runner-poster-art-Harrison-Ford.jpg" alt="">
<img src="http://demo.jb51.net/js/2018/html5-css3-3d-img-flash-codes/images/2017_alien_covenant_4k-5120x2880-1920x1080.jpg" alt="">
<img src="http://demo.jb51.net/js/2018/html5-css3-3d-img-flash-codes/images/robocop-1987-wallpaper-2.jpg" alt="">
<img src="http://demo.jb51.net/js/2018/html5-css3-3d-img-flash-codes/images/sJALsDXak4EehSg2F2y92rt5hPe.jpg" alt="">
<ul>
<li class="li_first"></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
<div class="button right"><span>></span></div>
<div class="button left"><span><</span></div>
</div>
<script>
var count = 0 ; //      count       
function run(){
count++;
count = count ==5?0:count;
$('#flash img').eq(count).fadeIn(300).siblings('img').fadeOut(300); //  eq   img,  count     ,        ,fadeIN     ,fadeOut   
$('#flash ul li').eq(count).css('background','#f40').siblings('li').css('background','#fff'); //              
}
function reverserun(){
count--;
count = count == -1?4:count;
$('#flash img').eq(count).fadeIn(300).siblings('img').fadeOut(300);
$('#flash ul li').eq(count).css('background','#f40').siblings('li').css('background','#fff');
}
var timer = setInterval(run,1000); //     
$('#flash').hover(function(){ //          
clearInterval(timer);
},function(){
timer = setInterval(run,1000);
})
$('#flash ul li').mouseenter(function(){ //        
count = $(this).index();
count = count ==5?0:count;
$('#flash img').eq(count).fadeIn(300).siblings('img').fadeOut(300);
$('#flash ul li').eq(count).css('background','#f40').siblings('li').css('background','#fff');
})
$('#flash .right').click(function(){ //          
run();
})
$('#flash .left').click(function(){ //          
reverserun();
})
</script>
</body>
</html>

興味のある友達はオンラインHTML/CSS/JavaScriptコードを使ってツールを実行できます。
jQuery関連の内容について興味がある読者は、当駅のテーマを調べてもいいです。「http://tools.jb51.net/code/HtmlJsRun」、「jQuery写真の操作の技巧の大全書」、「jQuery表の操作テクニックまとめ」、「jQuery切り替え特効とテクニックまとめ」、「jQuery拡張テクニックのまとめ」、「jQuery常用プラグインと使い方のまとめ」および「jQueryよくある経典効果のまとめ
ここで述べたように、皆さんのjQueryプログラムの設計に役に立ちます。