Skip to content

Instantly share code, notes, and snippets.

@akkunchoi
Created October 16, 2014 05:49
Show Gist options
  • Save akkunchoi/086bf27513f47bf99eba to your computer and use it in GitHub Desktop.
Save akkunchoi/086bf27513f47bf99eba to your computer and use it in GitHub Desktop.
jquery.ticker.js
(function(){
/*
thanks to http://on-ze.com/archives/618, http://on-ze.com/archives/618
<div class="ticker" rel="fade"><!-- fade or roll or slide-->
<ul>
<li>テキスト1</li>
<li>テキスト2</li>
<li>テキスト3</li>
</ul>
</div>
.ticker {
margin: 0 auto;
padding: 10px 0;
width: 600px;
text-align: left;
border: #ccc 1px solid;
background-color: #f6f6f6;
position: relative;
overflow: hidden;
}
.ticker ul {
width: 100%;
position: relative;
}
.ticker ul li {
width: 100%;
display: none;
}
*/
jQuery.fn.ticker = function(options){
//var $setElm = $('.ticker');
var effectSpeed = 1000;
var switchDelay = 6000;
var easing = 'swing';
if (options.effectSpeed){
effectSpeed = options.effectSpeed;
}
if (options.switchDelay){
switchDelay = options.switchDelay;
}
if (options.easing){
easing = options.easing;
}
if (typeof options.startOpacity === 'undefined'){
options.startOpacity = 0;
}
if (typeof options.endOpacity === 'undefined'){
options.endOpacity= 1;
}
$(this).each(function(){
var effectFilter = $(this).attr('rel');
var $targetObj = $(this);
var $targetUl = $targetObj.children('ul');
var $targetLi = $targetObj.find('li');
var $setList = $targetObj.find('li:first');
var ulWidth = $targetUl.width();
var listHeight = $targetLi.height();
$targetObj.css({height:(listHeight)});
$targetLi.css({top:'0',left:'0',position:'absolute'});
if(effectFilter == 'fade') {
$setList.css({display:'block',opacity:'0',zIndex:'98'}).stop().animate({opacity:'1'},effectSpeed,easing).addClass('showlist');
setInterval(function(){
var $activeShow = $targetObj.find('.showlist');
$activeShow.animate({opacity:'0'},effectSpeed,easing,function(){
$(this).next().css({display:'block',opacity:'0',zIndex:'99'}).animate({opacity:'1'},effectSpeed,easing).addClass('showlist').end().appendTo($targetUl).css({display:'none',zIndex:'98'}).removeClass('showlist');
});
},switchDelay);
} else if(effectFilter == 'roll') {
$setList.css({top:'3em',display:'block',opacity:'0',zIndex:'98'}).stop().animate({top:'0',opacity:'1'},effectSpeed,easing).addClass('showlist');
setInterval(function(){
var $activeShow = $targetObj.find('.showlist');
$activeShow.animate({top:'-3em',opacity:'0'},effectSpeed,easing).next().css({top:'3em',display:'block',opacity:'0',zIndex:'99'}).animate({top:'0',opacity:'1'},effectSpeed,easing).addClass('showlist').end().appendTo($targetUl).css({zIndex:'98'}).removeClass('showlist');
},switchDelay);
} else if(effectFilter == 'slide') {
$setList.css({left:(ulWidth),display:'block',opacity:options.startOpacity,zIndex:'98'}).stop().animate({left:'0',opacity:options.endOpacity},effectSpeed,easing).addClass('showlist');
setInterval(function(){
var $activeShow = $targetObj.find('.showlist');
$activeShow.animate({left:(-(ulWidth)),opacity:'0'},effectSpeed,easing).next().css({left:(ulWidth),display:'block',opacity:options.startOpacity,zIndex:'99'}).animate({left:'0',opacity:options.endOpacity},effectSpeed,easing).addClass('showlist').end().appendTo($targetUl).css({zIndex:'98'}).removeClass('showlist');
},switchDelay);
}
});
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment