Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加了y轴的轮播功能 #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
<div class="ck-slide">
<ul class="ck-slide-wrapper">
<li>
<a href="http://createthink.net"><img src="images/1.jpg" alt="" style="width:700px;"></a>
<a href="javascript:;"><img src="images/1.jpg" alt="" style="width:700px;"></a>
</li>
<li style="display:none">
<a href="http://www.oschina.net"><img src="images/2.jpg" alt="" style="width:700px;"></a>
<a href="javascript:;"><img src="images/2.jpg" alt="" style="width:700px;"></a>
</li>
<li style="display:none">
<a href="javascript:;"><img src="images/3.jpg" alt="" style="width:700px;"></a>
Expand Down Expand Up @@ -51,8 +51,8 @@
</div>
<script>
$('.ck-slide').ckSlide({
autoPlay:true
/*dir:"x"*/
autoPlay:true,
dir:"y"
});
</script>
</body>
Expand Down
88 changes: 70 additions & 18 deletions js/slide.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(function($){
$.fn.ckSlide = function(opts){
opts = $.extend({}, $.fn.ckSlide.opts, opts);

this.each(function(){
var slidewrap = $(this).find('.ck-slide-wrapper');
var slide = slidewrap.find('li');
Expand All @@ -10,12 +11,13 @@
var index = 0;
var time = null;
$(this).data('opts', opts);

// next
$(this).find('.ck-next').on('click', function(){
if(opts['isAnimate'] == true){
return;
}

var old = index;
if(index >= count - 1){
index = 0;
Expand All @@ -24,42 +26,50 @@
}
change.call(that, index, old);
});

// prev
$(this).find('.ck-prev').on('click', function(){
if(opts['isAnimate'] == true){
return;
}

var old = index;
if(index <= 0){
index = count - 1;
}else{
}else{
index--;
}
change.call(that, index, old);
});

//控制按钮绑定click事件
$(this).find('.ck-slidebox li').each(function(cindex){
$(this).on('click.slidebox', function(){
$(this).on('click.slidebox', function(){ //slidebox是命名空间
change.call(that, cindex, index);
index = cindex;
});
});
// focus clean auto play

// 鼠标滑过,清空定时器,并提高prev和next按钮的透明度
$(this).on('mouseover', function(){
if(opts.autoPlay){
clearInterval(time);
}
$(this).find('.ctrl-slide').css({opacity:0.6});
});
// leave

// 鼠标离开,设置定时器,并降低prev和next按钮的透明度
$(this).on('mouseleave', function(){
if(opts.autoPlay){
startAtuoPlay();
}
$(this).find('.ctrl-slide').css({opacity:0.15});
});

// 执行轮播
startAtuoPlay();


// auto play
function startAtuoPlay(){
if(opts.autoPlay){
Expand All @@ -70,16 +80,20 @@
}else{
index++;
}
change.call(that, index, old);
change.call(that, index, old);
}, 2000);
}
}
// 修正box

// 修正.ck-slidebox,保持水平居中
var box = $(this).find('.ck-slidebox');
box.css({
'margin-left':-(box.width() / 2)
})
});


// dir
// 如果存在dir参数
switch(opts.dir){
case "x":
opts['width'] = $(this).width();
Expand All @@ -93,26 +107,64 @@
slidewrap.wrap('<div class="ck-slide-dir"></div>');
slide.show();
break;
case "y":
opts['height'] = $(this).height();
slidewrap.css({
'height':count * opts['height']
});
slide.css({
'float':'left',
'position':'relative',
'height': opts['height']
});
slidewrap.wrap('<div class="ck-slide-dir"></div>');
slide.show();
break;
}
});
};

/**
* 换切图片
* @param {[num]} show [要显示的图片]
* @param {[num]} hide [要隐藏的图片]
*/
function change(show, hide){
var opts = $(this).data('opts');
if(opts.dir == 'x'){
var x = show * opts['width'];
$(this).find('.ck-slide-wrapper').stop().animate({'margin-left':-x}, function(){opts['isAnimate'] = false;});
opts['isAnimate'] = true
}else{
$(this).find('.ck-slide-wrapper li').eq(hide).stop().animate({opacity:0});
$(this).find('.ck-slide-wrapper li').eq(show).show().css({opacity:0}).stop().animate({opacity:1});
// if(opts.dir == 'x'){
// var x = show * opts['width'];
// $(this).find('.ck-slide-wrapper').stop().animate({'margin-left':-x}, function(){opts['isAnimate'] = false;});
// opts['isAnimate'] = true
// }else{
// $(this).find('.ck-slide-wrapper li').eq(hide).stop().animate({opacity:0});
// $(this).find('.ck-slide-wrapper li').eq(show).show().css({opacity:0}).stop().animate({opacity:1});
// }

switch (opts.dir) {
case 'x':
var x = show * opts['width'];
$(this).find('.ck-slide-wrapper').stop().animate({'margin-left':-x}, function(){opts['isAnimate'] = false;});
opts['isAnimate'] = true;
break;
case 'y':
var y = show * opts['height'];
$(this).find('.ck-slide-wrapper').stop().animate({'margin-top':-y}, function(){opts['isAnimate'] = false;});
opts['isAnimate'] = true;
break;
default:
$(this).find('.ck-slide-wrapper li').eq(hide).stop().animate({opacity:0});
$(this).find('.ck-slide-wrapper li').eq(show).show().css({opacity:0}).stop().animate({opacity:1});
break;
}

$(this).find('.ck-slidebox li').removeClass('current');
$(this).find('.ck-slidebox li').eq(show).addClass('current');
}

$.fn.ckSlide.opts = {
autoPlay: false,
dir: null,
isAnimate: false
};

})(jQuery);