制作一个简单的图片幻灯jQuery插件
网络上有很多漂亮的Javascript幻灯片脚本、图片轮播展示及jQuery相册展示插件。但它们的教程感觉很复杂。今天给朋友们介绍一种很简单的图片幻灯jQuery。我不懂Javascript,只是结合我个人的使用体验,讲讲如何制作,实现图片幻灯播放效果。
我是在wordpress程序下应用的,其他开源程序应该也可以。
1、header.php中添加以下代码:
<script type="text/javascript">
$(document).ready(function() {
//move he last list item before the first item. The purpose of this is if the user clicks to slide left he will be able to see the last item.
$('#carousel_ul li:first').before($(‘#carousel_ul li:last’));
//when user clicks the image for sliding right
$('#right_scroll img').click(function(){
//get the width of the items ( i like making the jquery part dynamic, so if you change the width in the css you won't have o change it here too ) '
var item_width = $('#carousel_ul li').outerWidth() + 10;
//calculae the new left indent of the unordered list
var left_indent = parseInt($('#carousel_ul').css(‘left’)) – item_width;
//make the sliding effect using jquery's anumate function '
$('#carousel_ul:not(:animated)').animate({‘left’ : left_indent},500,function(){
//get the first list item and put it after the last list item (that's how the infinite effects is made) '
$('#carousel_ul li:last').after($(‘#carousel_ul li:first’));
//and get the left indent to the default -210px
$('#carousel_ul').css({‘left’ : ‘-210px’});
});
});
//when user clicks the image for sliding left
$('#left_scroll img').click(function(){
var item_width = $('#carousel_ul li').outerWidth() + 10;
/* same as for sliding right except that it's current left indent + the item width (for the sliding right it's – item_width) */
var left_indent = parseInt($('#carousel_ul').css(‘left’)) + item_width;
$('#carousel_ul:not(:animated)').animate({‘left’ : left_indent},500,function(){
/* when sliding to left we are moving the last item before the first list item */
$('#carousel_ul li:first').before($(‘#carousel_ul li:last’));
/* and again, when we make that change we are setting the left indent of our unordered list to the default -210px */
$('#carousel_ul').css({‘left’ : ‘-210px’});
});
});
});
</script>
2、style.css 中添加
float:left; /* important for inline positioning */
width:630px; /* important (this width = width of list item(including margin) * items shown */
position:relative;
overflow: hidden; /* important (hide the items outside the div) */
background: #F0F0F0;
}
#carousel_ul {
position:relative;
overflow:hidden;
left:-210px; /* important (this should be negative number of list items width(including margin) */
list-style-type: none;
margin: 0px;
padding: 0px;
width:9999px; /* important */
padding-bottom:10px;
}
#carousel_ul li{
float: left; /* important for inline positioning of the list items */
width:200px; /* fixed width, important */
padding:0px;
height:110px;
background: #000000;
margin-top:10px;
margin-bottom:10px;
margin-left:5px;
margin-right:5px;
}
#carousel_ul li img {
.margin-bottom:-4px; /* IE is making a 4px gap bellow an image inside of an anchor (<a href…>) so this is to fix that*/
cursor:pointer;
cursor: hand;
border:0px;
}
#left_scroll, #right_scroll{
float:left;
height:130px;
width:15px;
background: #C0C0C0;
}
#left_scroll img, #right_scroll img{
cursor: pointer;
cursor: hand;
}
你需要根据自己的theme适当调整,如width、height,background等。这里特别强调left:-210px这个值,它等于width+ margin-left + margin-right
3、添加以下HTML代码到适当位置。我的是添加到sidebar.php中
<div id='left_scroll'><img src=’left.png’ /></div>
<div id='carousel_inner'>
<ul id='carousel_ul'>
<li><a href='#'><img src=’item1.png’ /></a></li>
<li><a href='#'><img src=’item2.png’ /></a></li>
<li><a href='#'><img src=’item3.png’ /></a></li>
<li><a href='#'><img src=’item4.png’ /></a></li>
<li><a href='#'><img src=’item5.png’ /></a></li>
</ul>
</div>
<div id='right_scroll'><img src=’right.png’ /></div>
</div>
需要的两个image到我的网盘下载
就这些,很简单吧!相信您一看就明白。
相关阅读: