HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URL's added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<div id="slider-wrapper">
<div id="slider-container">
<div class="slide">SLIDE 1</div>
<div class="slide">SLIDE 2</div>
<div class="slide">SLIDE 3</div>
<div class="slide">SLIDE 4</div>
<div class="slide">SLIDE 5</div>
</div>
<ul id="menu">
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
body
{
font: 16px "Trebuchet MS", Arial, Helvetica, sans-serif;
}
#slider-wrapper
{
width: 800px;
/*height: 600px;*/
background: #000;
color: #fff;
margin: 50px auto;
position: relative;
overflow: hidden;
}
#slider-container
{
width: 100%;
height: 580px;
position: relative;
}
.slide
{
position: absolute;
background-color: #0000C4;
width: 94%;
height: 94%;
top:3%;
left: 3%;
font-size: 40px;
font-weight: bold;
text-align: center;
}
#menu
{
position: relative;
list-style: none;
padding: 5px 0;
margin: 0;
background-color: #777;
text-align: center;
color: #fff;
}
#menu li
{
display: inline-block;
padding:10px;
background-color: #555;
border: solid 1px #fff;
border-radius: 50%;
cursor: pointer;
}
// this numbers indicate the index position of the slides to animate in and out. by default starts in 0 for the slide that will be animated out and 1 for the slide that will be animated in
var slideNumber = 0,// current slide. visible slide
nextSlide = 1,// next slide
// the DOM elements
wrapper = $("#slider-wrapper"),
// menu elements
menuLi = $("#menu li"),
// slides
slides = $(".slide"),
totalSlides = slides.length,
// define a dynamic pause time just in case
pauseTime = 2,
// define the slide animation duration
duration = 1,
// timer function that will be paused on mouse event, mainly mouse over. this also will be killed on click event
timerFunction = TweenLite.delayedCall(pauseTime, changeSlide),
// boolean to inform if there's a slide currently animating. useful for the click events. if a slide is animating the click event won't work as expected, it won't trigger the function to change the slide. if no slide is animating the slide corresponding to the button's index will be animated
slideAnimating = false;
// set the initial position of the slides to the left of the container. the first slide won't be considered because is the visible slide when the effect starts
TweenLite.set(slides.not(":eq(0)"), {left:"100%"});
TweenLite.set(menuLi[0], {backgroundColor:"#222"});
function changeSlide()
{
// set the boolean to indicate that a slide is currently animating. prevents the click button to call the function while a slide is animating
slideAnimating = true;
var menuLine = new TimelineLite();
console.log( "slide out => " + slideNumber );
console.log( "slide IN => " + nextSlide + "\n" );
// this will animate the menu item background color
menuLine
.to(menuLi[slideNumber], duration,
{
backgroundColor:"#555", className:"+=active"
},0)
.to(menuLi[nextSlide], duration,
{
backgroundColor:"#222", className:"+=active"
},0);
// this will animate the currently visible slide to the far left.
TweenLite.to(slides[slideNumber], duration,
{
left:"-100%",
onComplete:function()
{
// the slide animation is complete. change the boolean to indicate that no slide is animating. the click event on a button will trigger the animation on the corresponding slide
slideAnimating = false;
// if the mouse is over during the slide animation it should be paused right after being created and resumed once the mouse leaves the container to resume the autoplay feature.
// here there's has to be some conditional logic to prevent the timer to start again if the mouse is over the container, for that check the special class added to the
timerFunction.restart(true);
// check if the mouse is on the element, therefore the timer function should be paused immediately. the mouseover event pauses the timer function, but if mouse goes over the container while the slide was animating the timer function will start even with the mouse in the container. to prevent that a class is added to the container, if it has that class the timer should be paused.
if( wrapper.hasClass( "mouse-over" ) )
{
timerFunction.pause();
}
// finally set the slide that was animated out to the far right of the container
TweenLite.set(this.target, {left:"100%"});
}// ON COMPLETE CALLBACK END
});//CURRENTLY VISIBLE SLIDE ANIMATION END
// NEXT SLIDE ANIMATION START
TweenLite.to(slides[nextSlide], duration,
{
left:"3%"
});
//
if( nextSlide < totalSlides -1 )
{
// set the index of the slide that will be animated-out in the next execution
slideNumber = nextSlide;
// set the index of the slide that will be animated-in in hte next execution
nextSlide++;
}
else
{
// set the index of the slide that will be animated-out in the next execution
slideNumber = nextSlide;
// if the visible slide is the last one set the index for the slide that will be animated-in to 0, that means the first slide
nextSlide = 0;
console.log("first slide!!");
}
}
/*
---------------------------------------------------
MOUSE OVER AND OUT EVENTS
---------------------------------------------------
*/
// in order to check if the mouse is over when the slide is animating add a class to the container.
wrapper.mouseover(function(e)
{
TweenLite.set(this, {className:"+=mouse-over"});
timerFunction.pause();
});
wrapper.mouseout(function(e)
{
TweenLite.set(this, {className:"-=mouse-over"});
timerFunction.play();
});
/*
---------------------------------------------------
MENU ITEM CLICK EVENT
---------------------------------------------------
*/
$.each(menuLi, function(i,e)
{
var hoverLine = new TimelineLite({paused:true});
hoverLine.to(e, .3, {backgroundColor:"#222"});
e.hoverLine = hoverLine;
});
menuLi.click(function(e)
{
console.log( slideAnimating );
// get the button being clicked index position. this will be set as the slide to be animated.
var btnIndex = menuLi.index(this);
// if there's no slide animating pause the timer function. select the button's index and set the slide number according to it. call the function to animate in the corresponding slide and finally restart the timer fuction to cntinue with the autoplay feature. also avoid executing the function if the user clicks on the button corresponding to the currently visible slide
if(!slideAnimating && btnIndex !== slideNumber)
{
// pause the timer function. with this avoid the slide being animated out too soon. if the timer has advanced for some time the slider being animated-in by the click event, will be visible for less time than the expected. unwanted behaviour. the timer function will be restarted in the change slide function so the pause time between slides animations will be the usual one.
timerFunction.pause();
// set the correct index to show the slide corresponding to the button being clicked. otherwise the slide animated in will be the next one sequentially and not necesarly the one indicated by the button's index position
nextSlide = btnIndex;
// call the function to change animate the slides
changeSlide();
}
})
// hover event for menu items
.mouseover(function(e)
{
this.hoverLine.play();
})
.mouseout(function(e)
{
this.hoverLine.reverse();
});
Also see: Tab Triggers