Skip to content

Instantly share code, notes, and snippets.

@proxymoron
Created July 24, 2015 16:08
Show Gist options
  • Select an option

  • Save proxymoron/d236e425b3aa86997ffc to your computer and use it in GitHub Desktop.

Select an option

Save proxymoron/d236e425b3aa86997ffc to your computer and use it in GitHub Desktop.
jQuery Text Slider
<h1>Here is my take on the text slider from the <a href="https://www.kickstarter.com/discover">kickstarter/discover</a> page</h1>
<div class="wrap">
<ul id="sentence">
<li id="first">Here is a </li>
<ul id="adjList"></ul>
<li id="last"> statement.</li>
</ul>
</div>
<button id="stop">Stop</button>
<button id="look">Take a look under the hood <span>(toggle)</span></button>
////////////////////////////
// Twitter: @mikedevelops
////////////////////////////
// add as many adjectives as you like!
var adjs = ["very cool", "really exciting", " super awesome", "jQuery sliding"],
sentence = $('#sentence'),
adjList = $('#adjList'),
stop = false;
// function to return the most up-to-date
// version of our adjective list
function getList() {
return $('.adj');
}
// function to build the adjective list
// args: adjective array
function build(arr) {
for (i=0; i<arr.length; i++) {
var item = "<li class='adj'>";
$(adjList).append(item + arr[i] + "</li>");
}
}
// function to resize adjective list
// args: adjective list item
function resize(el) {
$(adjList).animate({
width: $(el).width(),
}, 200);
}
// function to add slide-in transition
// args: element that needs to slide
function slideIn(el) {
// duration slide is on screen
var hold = 1000;
// resize area to sliding element
resize($(el));
// add slide-in class
$(el).addClass('slide-in');
// after 'hold' duration slide-out current item
// then slide in next item
setTimeout(function(){
// check to see if loop should continue
if (stop === true) {
stop = false;
return;
}
// slide current element out
slideOut(el);
// slide in next element in queue
slideIn($(el).next());
}, hold);
}
// function to add slide-out transition
// args: element that needs to slide
function slideOut(el) {
// remove current class and add slide-out transition
$(el).removeClass('slide-in').addClass('slide-out');
// wait for slide tramsition to finish then
// call 'change' function
setTimeout(function(){
change();
}, 200);
}
// function to re-arrange adjective list
function change() {
// store last item index
var i = adjs.length - 1;
// detach element that has slide-out class
// put to the bottom of the list after
// the last item
$('.adj:eq(' + i + ')').after($('.slide-out').detach());
// remove class to send element back to original position
$('.adj').removeClass('slide-out');
}
// build slider
build(adjs);
// init loop
slideIn(getList()[0]);
$('#stop').click(function(){
// stop/start loop
if ($(this).html() === "Start") {
$(this).html("Stop");
slideIn(getList()[0]);
} else {
stop = true;
$(this).html("Start");
}
});
$('#look').click(function(){
if (!$(sentence).hasClass('overflow')) {
$(sentence).addClass('overflow');
} else {
$(sentence).removeClass('overflow');
}
});
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
@import url(http://fonts.googleapis.com/css?family=PT+Sans:400,700);
html {
background: linear-gradient(-134deg, #32DE62 0%, #8CE2A4 100%) no-repeat;
width: 100%;
height: 100%;
font-family: 'PT Sans', Helvetica, Arial, sans-serif;
font-weight: 200;
font-size: 30px;
text-align: center;
min-width: 500px;
}
h1 {
padding: 50px;
line-height: 1.2em;
}
a {
color: #4A90E2;
text-decoration: none;
}
button {
margin-top: 25px;
span {
font-size: .7em;
font-style: italic;
}
}
.wrap {
background: white;
border-radius: 10px;
padding: 40px 0;
margin: 25px 0;
}
#sentence {
overflow: hidden;
padding: 20px;
& > li,
& > ul {
display: inline;
}
}
#adjList {
overflow: visible !important;
text-align: left;
display: inline;
position: relative;
height: 16px;
}
.adj {
white-space: nowrap;
list-style: none;
position: absolute;
line-height: .3em;
transform: translateY(60px);
}
$speed: .3s;
.slide-in {
transform: translateY(0);
transition: $speed;
}
.slide-out {
transform: translateY(-60px);
transition: $speed;
}
button {
cursor: pointer;
border: none;
color: white;
padding: 10px 5px;
background: none;
font-weight: normal;
font-size: 1em;
border-bottom: 2px solid white;
margin-right: 30px;
&:last-of-type {
margin-right: 0;
}
}
.overflow {
overflow: visible !important;
border: 1px solid red;
}
.border {
border: 1px soild red;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment