jQueryでよくあるCarouselですが、最後の画像まで行くと最初の画像まで巻き戻すものが多いので最後にもう一つ最初の画像を加えてループするようにしてみました。
jQuery Carousel loopですが、どこかでCarouselをダウンロードして改造したのですがリンク貼ろうとしたら元のURLを忘れてしまいました。知ってたら教えてください。
jQueryでよくあるCarouselですが、最後の画像まで行くと最初の画像まで巻き戻すものが多いので最後にもう一つ最初の画像を加えてループするようにしてみました。
右上の●をクリックして各画像にジャンプできます。
右上の●をクリックして各画像にジャンプできます。
■HTML
<div class="main_view diary"> <div class="window"> <div class="image_reel"> <img class="size-full wp-image-834 alignnone" title="gu001" src="http://www.pictron.net/wp-content/uploads/2012/01/gu001.jpg" alt="" width="420" height="393" /> <img class="alignnone size-full wp-image-835" title="gu002" src="http://www.pictron.net/wp-content/uploads/2012/01/gu002.jpg" alt="" width="420" height="393" /> <img class="alignnone size-full wp-image-836" title="gu003" src="http://www.pictron.net/wp-content/uploads/2012/01/gu003.jpg" alt="" width="420" height="393" /> <img class="alignnone size-full wp-image-834" title="gu001" src="http://www.pictron.net/wp-content/uploads/2012/01/gu001.jpg" alt="" width="420" height="393" /> </div> </div> <div class="paging"> <a href="#" rel="1">●</a> <a href="#" rel="2">●</a> <a href="#" rel="3">●</a> <a style="display: none;" href="#" rel="4">●</a> </div> <div class="ttl"> <img class="alignnone size-full wp-image-837" title="ttl" src="http://www.pictron.net/wp-content/uploads/2012/01/ttl.png" alt="" width="93" height="18" /> </div> </div>
■ CSS
@charset "UTF-8";
/* CSS Document */
/*--Main Container--*/
.main_view {
float: left;
position: relative;
}
/*--Window/Masking Styles--*/
.diary .window {
height:392px;
width: 420px;
overflow: hidden; /*--Hides anything outside of the set width/height--*/
position: relative;
}
.main_view .image_reel {
position: absolute;
top: 0; left: 0;
}
.main_view .image_reel img {float: left;margin:0;padding:0;}
/*--Paging Styles--*/
.main_view .paging {
position: absolute;
top: 0px; right: 0px;
width: 75px; height:27px;
z-index: 100; /*--Assures the paging stays on the top layer--*/
text-align: center;
line-height: 20px;
display: none; /*--Hidden by default, will be later shown with jQuery--*/
}
.main_view .ttl {
position: absolute;
bottom: 10px; right: 10px;
z-index: 100; /*--Assures the paging stays on the top layer--*/
}
.main_view .paging a {
padding: 2px;
text-decoration: none;
color: #231815;
}
.main_view .paging a.active {
font-weight: bold;
color:#BAB9B9;
}
.main_view .paging a:hover {font-weight: bold;}
■ Javascript
wordpressでなければ
jQuery(document).ready(function($)
のところは
$(document).ready(function()
で大丈夫です。
wordpressでなければ
jQuery(document).ready(function($)
のところは
$(document).ready(function()
で大丈夫です。
jQuery(document).ready(function($) {
//Set Default State of each portfolio piece
$(".diary .paging").show();
$(".diary .paging a:first").addClass("active");
//Get size of images, how many there are, then determin the size of the image reel.
var imageWidth = $(".window").width();
//var imageSum = $(".window .image_reel img").size();
var imageSum = 4;
var imageReelWidth = imageWidth * imageSum;
//Adjust the image reel to its new size
$(".window .image_reel").css({'width' : imageReelWidth});
//Paging + Slider Function
rotate = function(){
var triggerID = $active.attr("rel") - 1; //Get number of times to slide
var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide
$(".diary .paging a").removeClass('active'); //Remove all active class
if($active.attr("rel") == '4'){
$('.diary .paging a:first').addClass('active');
}else{
$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)
}
//Slider Animation
$(".window .image_reel").animate({
left: -image_reelPosition
}, 700 ,function () {
if($active.attr("rel") == '4'){
$(".window .image_reel").animate({
left: 0
}, 1 );
}
});
};
//Rotation + Timing Event
rotateSwitch = function(){
play = setInterval(function(){ //Set timer - this will repeat itself every 3 seconds
$active = $('.diary .paging a.active').next();
if ( $active.length === 0) { //If paging reaches the end...
$active = $('.diary .paging a:first'); //go back to first
}
if($active.length !== 0){
rotate(); //Trigger the paging and slider function
}
}, 5000); //Timer speed in milliseconds (3 seconds)
};
rotateSwitch(); //Run function on launch
//On Hover
$(".image_reel a").hover(function() {
clearInterval(play); //Stop the rotation
}, function() {
rotateSwitch(); //Resume rotation
});
//On Click
$(".diary .paging a").click(function() {
$active = $(this); //Activate the clicked paging
//Reset Timer
clearInterval(play); //Stop the rotation
rotate(); //Trigger rotation immediately
rotateSwitch(); // Resume rotation
return false; //Prevent browser jump to link anchor
});
//On Click
});









