Created
December 18, 2018 11:05
-
-
Save kartick14/9848c67bc15e8d0a6a357d02507a0444 to your computer and use it in GitHub Desktop.
jquery vertical text slide
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="pre_header_text visible-web destop_screen" style="background: #000000 none repeat scroll 0 0; color: #ffffff;"> | |
<div class="rw-words rw-words-1 cd-headline slide"> | |
<span class="cd-words-wrapper" style="width: 174px;"> | |
<b class="is-hidden">FREE U.S. Shipping!</b> | |
<b class="is-hidden">FREE U.S. Shipping! 11</b> | |
<b class="is-hidden">FREE U.S. Shipping! 222</b> | |
<b class="is-hidden">FREE U.S. Shipping! 3333</b> | |
<b class="is-hidden">FREE U.S. Shipping! 44444</b> | |
<b class="is-visible">Customize your box options now available!</b> | |
</span> | |
</div> | |
</div> | |
<style type="text/css"> | |
/*---------------------------*/ | |
.cd-words-wrapper b { | |
font-size: 15px; | |
width: 100% !important; | |
color: #fff | |
} | |
.cd-words-wrapper { | |
text-align: center !important; | |
width: auto !important; | |
} | |
@media all and (max-width: 768px) { | |
.pre_header_text { | |
font-size: 13px; | |
height: 100%; | |
letter-spacing: 2px; | |
margin-top: 15px !important; | |
padding: 8px | |
} | |
.pre_header_text { | |
font-size: 13px; | |
letter-spacing: 2px; | |
margin-top: 0; | |
padding: 8px | |
} | |
img#product-featured-image { | |
position: relative !important; | |
} | |
} | |
@media all and (max-width: 480px) { | |
.cd-words-wrapper b { | |
font-size: 15px; | |
width: 100% !important; | |
color: #fff; | |
white-space: normal !important | |
} | |
} | |
/*=======*/ | |
.cd-title { | |
position: relative; | |
height: 160px; | |
line-height: 230px; | |
text-align: center; | |
} | |
.cd-title h1 { | |
font-size: 2.4rem; | |
font-weight: 700; | |
} | |
@media only screen and (min-width: 768px) { | |
.cd-title { | |
line-height: 250px; | |
} | |
} | |
@media only screen and (min-width: 1170px) { | |
.cd-title { | |
height: 200px; | |
line-height: 300px; | |
} | |
.cd-title h1 { | |
font-size: 3rem; | |
} | |
} | |
.cd-intro { | |
width: 90%; | |
max-width: 768px; | |
text-align: center; | |
} | |
.cd-intro { | |
margin: 4em auto; | |
} | |
@media only screen and (min-width: 768px) { | |
.cd-intro { | |
margin: 5em auto; | |
} | |
} | |
@media only screen and (min-width: 1170px) { | |
.cd-intro { | |
margin: 6em auto; | |
} | |
} | |
.cd-headline { | |
font-size: 15px; | |
line-height: 1.2; | |
} | |
@media only screen and (min-width: 768px) { | |
.cd-headline { | |
font-size: 15px; | |
font-weight: 300; | |
} | |
} | |
@media only screen and (min-width: 1170px) { | |
.cd-headline { | |
font-size: 15px; | |
} | |
} | |
.cd-words-wrapper { | |
display: inline-block; | |
position: relative; | |
text-align: center; | |
} | |
.cd-words-wrapper b { | |
display: inline-block; | |
position: absolute; | |
white-space: nowrap; | |
left: 0; | |
top: 0; | |
} | |
.cd-words-wrapper b.is-visible { | |
position: relative; | |
} | |
.no-js .cd-words-wrapper b { | |
opacity: 0; | |
} | |
.no-js .cd-words-wrapper b.is-visible { | |
opacity: 1; | |
} | |
.pre_header_text { | |
/* background: #000000 none repeat scroll 0 0; */ | |
display: block; | |
font-size: 15px; | |
/*height: 100%;*/ | |
letter-spacing: 2px; | |
margin-top: 0; | |
overflow: visible; | |
position: relative; | |
text-align: center; | |
top: -15px; | |
padding: 5px 0px; | |
/* color: #fff; */ | |
} | |
.cd-headline.slide b { | |
opacity: 0; | |
top: .2em; | |
} | |
/* -------------------------------- | |
xslide | |
-------------------------------- */ | |
.cd-headline.slide span { | |
display: inline-block; | |
padding: .2em 0; | |
} | |
.cd-headline.slide .cd-words-wrapper { | |
overflow: hidden; | |
vertical-align: top; | |
} | |
.cd-headline.slide b { | |
opacity: 0; | |
top: .2em; | |
} | |
.cd-headline.slide b.is-visible { | |
top: 0; | |
opacity: 1; | |
-webkit-animation: slide-in 0.6s; | |
-moz-animation: slide-in 0.6s; | |
animation: slide-in 0.6s; | |
} | |
.cd-headline.slide b.is-hidden { | |
-webkit-animation: slide-out 0.6s; | |
-moz-animation: slide-out 0.6s; | |
animation: slide-out 0.6s; | |
} | |
@-webkit-keyframes slide-in { | |
0% { | |
opacity: 0; | |
-webkit-transform: translateY(-100%); | |
} | |
60% { | |
opacity: 1; | |
-webkit-transform: translateY(20%); | |
} | |
100% { | |
opacity: 1; | |
-webkit-transform: translateY(0); | |
} | |
} | |
@-moz-keyframes slide-in { | |
0% { | |
opacity: 0; | |
-moz-transform: translateY(-100%); | |
} | |
60% { | |
opacity: 1; | |
-moz-transform: translateY(20%); | |
} | |
100% { | |
opacity: 1; | |
-moz-transform: translateY(0); | |
} | |
} | |
@keyframes slide-in { | |
0% { | |
opacity: 0; | |
-webkit-transform: translateY(-100%); | |
-moz-transform: translateY(-100%); | |
-ms-transform: translateY(-100%); | |
-o-transform: translateY(-100%); | |
transform: translateY(-100%); | |
} | |
60% { | |
opacity: 1; | |
-webkit-transform: translateY(20%); | |
-moz-transform: translateY(20%); | |
-ms-transform: translateY(20%); | |
-o-transform: translateY(20%); | |
transform: translateY(20%); | |
} | |
100% { | |
opacity: 1; | |
-webkit-transform: translateY(0); | |
-moz-transform: translateY(0); | |
-ms-transform: translateY(0); | |
-o-transform: translateY(0); | |
transform: translateY(0); | |
} | |
} | |
@-webkit-keyframes slide-out { | |
0% { | |
opacity: 1; | |
-webkit-transform: translateY(0); | |
} | |
60% { | |
opacity: 0; | |
-webkit-transform: translateY(120%); | |
} | |
100% { | |
opacity: 0; | |
-webkit-transform: translateY(100%); | |
} | |
} | |
@-moz-keyframes slide-out { | |
0% { | |
opacity: 1; | |
-moz-transform: translateY(0); | |
} | |
60% { | |
opacity: 0; | |
-moz-transform: translateY(120%); | |
} | |
100% { | |
opacity: 0; | |
-moz-transform: translateY(100%); | |
} | |
} | |
@keyframes slide-out { | |
0% { | |
opacity: 1; | |
-webkit-transform: translateY(0); | |
-moz-transform: translateY(0); | |
-ms-transform: translateY(0); | |
-o-transform: translateY(0); | |
transform: translateY(0); | |
} | |
60% { | |
opacity: 0; | |
-webkit-transform: translateY(120%); | |
-moz-transform: translateY(120%); | |
-ms-transform: translateY(120%); | |
-o-transform: translateY(120%); | |
transform: translateY(120%); | |
} | |
100% { | |
opacity: 0; | |
-webkit-transform: translateY(100%); | |
-moz-transform: translateY(100%); | |
-ms-transform: translateY(100%); | |
-o-transform: translateY(100%); | |
transform: translateY(100%); | |
} | |
} | |
</style> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script> | |
<script type="text/javascript"> | |
jQuery(document).ready(function(){ | |
//set animation timing | |
var animationDelay = 2500, | |
//loading bar effect | |
barAnimationDelay = 3800, | |
barWaiting = barAnimationDelay - 3000, //3000 is the duration of the transition on the loading bar - set in the scss/css file | |
//letters effect | |
lettersDelay = 50, | |
//type effect | |
typeLettersDelay = 150, | |
selectionDuration = 500, | |
typeAnimationDelay = selectionDuration + 800, | |
//clip effect | |
revealDuration = 600, | |
revealAnimationDelay = 1500; | |
initHeadline(); | |
function initHeadline() { | |
//insert <i> element for each letter of a changing word | |
singleLetters($('.cd-headline.letters').find('b')); | |
//initialise headline animation | |
animateHeadline($('.cd-headline')); | |
} | |
function singleLetters($words) { | |
$words.each(function(){ | |
var word = $(this), | |
letters = word.text().split(''), | |
selected = word.hasClass('is-visible'); | |
for (i in letters) { | |
if(word.parents('.rotate-2').length > 0) letters[i] = '<em>' + letters[i] + '</em>'; | |
letters[i] = (selected) ? '<i class="in">' + letters[i] + '</i>': '<i>' + letters[i] + '</i>'; | |
} | |
var newLetters = letters.join(''); | |
word.html(newLetters).css('opacity', 1); | |
}); | |
} | |
function animateHeadline($headlines) { | |
var duration = animationDelay; | |
$headlines.each(function(){ | |
var headline = $(this); | |
if(headline.hasClass('loading-bar')) { | |
duration = barAnimationDelay; | |
setTimeout(function(){ headline.find('.cd-words-wrapper').addClass('is-loading') }, barWaiting); | |
} else if (headline.hasClass('clip')){ | |
var spanWrapper = headline.find('.cd-words-wrapper'), | |
newWidth = spanWrapper.width() + 10 | |
spanWrapper.css('width', newWidth); | |
} else if (!headline.hasClass('type') ) { | |
//assign to .cd-words-wrapper the width of its longest word | |
var words = headline.find('.cd-words-wrapper b'), | |
width = 0; | |
words.each(function(){ | |
var wordWidth = $(this).width(); | |
if (wordWidth > width) width = wordWidth; | |
}); | |
headline.find('.cd-words-wrapper').css('width', width); | |
}; | |
//trigger animation | |
setTimeout(function(){ hideWord( headline.find('.is-visible').eq(0) ) }, duration); | |
}); | |
} | |
function hideWord($word) { | |
var nextWord = takeNext($word); | |
if($word.parents('.cd-headline').hasClass('type')) { | |
var parentSpan = $word.parent('.cd-words-wrapper'); | |
parentSpan.addClass('selected').removeClass('waiting'); | |
setTimeout(function(){ | |
parentSpan.removeClass('selected'); | |
$word.removeClass('is-visible').addClass('is-hidden').children('i').removeClass('in').addClass('out'); | |
}, selectionDuration); | |
setTimeout(function(){ showWord(nextWord, typeLettersDelay) }, typeAnimationDelay); | |
} else if($word.parents('.cd-headline').hasClass('letters')) { | |
var bool = ($word.children('i').length >= nextWord.children('i').length) ? true : false; | |
hideLetter($word.find('i').eq(0), $word, bool, lettersDelay); | |
showLetter(nextWord.find('i').eq(0), nextWord, bool, lettersDelay); | |
} else if($word.parents('.cd-headline').hasClass('clip')) { | |
$word.parents('.cd-words-wrapper').animate({ width : '2px' }, revealDuration, function(){ | |
switchWord($word, nextWord); | |
showWord(nextWord); | |
}); | |
} else if ($word.parents('.cd-headline').hasClass('loading-bar')){ | |
$word.parents('.cd-words-wrapper').removeClass('is-loading'); | |
switchWord($word, nextWord); | |
setTimeout(function(){ hideWord(nextWord) }, barAnimationDelay); | |
setTimeout(function(){ $word.parents('.cd-words-wrapper').addClass('is-loading') }, barWaiting); | |
} else { | |
switchWord($word, nextWord); | |
setTimeout(function(){ hideWord(nextWord) }, animationDelay); | |
} | |
} | |
function showWord($word, $duration) { | |
if($word.parents('.cd-headline').hasClass('type')) { | |
showLetter($word.find('i').eq(0), $word, false, $duration); | |
$word.addClass('is-visible').removeClass('is-hidden'); | |
} else if($word.parents('.cd-headline').hasClass('clip')) { | |
$word.parents('.cd-words-wrapper').animate({ 'width' : $word.width() + 10 }, revealDuration, function(){ | |
setTimeout(function(){ hideWord($word) }, revealAnimationDelay); | |
}); | |
} | |
} | |
function hideLetter($letter, $word, $bool, $duration) { | |
$letter.removeClass('in').addClass('out'); | |
if(!$letter.is(':last-child')) { | |
setTimeout(function(){ hideLetter($letter.next(), $word, $bool, $duration); }, $duration); | |
} else if($bool) { | |
setTimeout(function(){ hideWord(takeNext($word)) }, animationDelay); | |
} | |
if($letter.is(':last-child') && $('html').hasClass('no-csstransitions')) { | |
var nextWord = takeNext($word); | |
switchWord($word, nextWord); | |
} | |
} | |
function showLetter($letter, $word, $bool, $duration) { | |
$letter.addClass('in').removeClass('out'); | |
if(!$letter.is(':last-child')) { | |
setTimeout(function(){ showLetter($letter.next(), $word, $bool, $duration); }, $duration); | |
} else { | |
if($word.parents('.cd-headline').hasClass('type')) { setTimeout(function(){ $word.parents('.cd-words-wrapper').addClass('waiting'); }, 200);} | |
if(!$bool) { setTimeout(function(){ hideWord($word) }, animationDelay) } | |
} | |
} | |
function takeNext($word) { | |
return (!$word.is(':last-child')) ? $word.next() : $word.parent().children().eq(0); | |
} | |
function takePrev($word) { | |
return (!$word.is(':first-child')) ? $word.prev() : $word.parent().children().last(); | |
} | |
function switchWord($oldWord, $newWord) { | |
$oldWord.removeClass('is-visible').addClass('is-hidden'); | |
$newWord.removeClass('is-hidden').addClass('is-visible'); | |
} | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment