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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.
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.
If the stylesheet 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 CSS 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.
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 class="container">
<div class="inner">
<h1>Image Comparison Slider</h1>
<h3>A cool way to show diferences between two image, using CSS3 and jQuery.</h3>
<!-- COMPARISON SLIDER CODE START -->
<div class="comparison-slider-wrapper">
<!-- Comparison Slider - this div contain the slider with the individual images captions -->
<div class="comparison-slider">
<div class="overlay">And I am the <strong>after</strong> image.</div>
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/751678/marioPhoto-2.jpg" alt="marioPhoto 2">
<!-- Div containing the image layed out on top from the left -->
<div class="resize">
<div class="overlay">I am the <strong>before</strong> image.</div>
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/751678/marioPhoto-1.jpg" alt="marioPhoto 1">
</div>
<!-- Divider where user will interact with the slider -->
<div class="divider"></div>
</div>
<!-- All global captions if exist can go on the div bellow -->
<div class="caption">I am the caption for the comparison slider and i can give in more detail a context off what you looking at, in this case we are looking at a demo of the comparison slider :)</div>
</div>
<!-- COMPARISON SLIDER CODE END -->
<div class="split">
<p>Cupcake ipsum dolor sit amet tart sesame snaps I love tart. Macaroon I love chocolate cake cupcake wafer oat cake carrot cake. Halvah lemon drops icing. Jelly beans I love lollipop danish. I love chupa chups gummi bears donut toffee. Fruitcake halvah chocolate bar chocolate. Apple pie danish liquorice sugar plum apple pie cheesecake. I love dessert caramels carrot cake cheesecake carrot cake dessert.</p>
<p>Icing biscuit I love pudding I love. Tart tart marshmallow fruitcake cookie bear claw jujubes I love. Soufflé I love candy fruitcake pie cake gingerbread chocolate bar. Sesame snaps pudding candy. Pudding croissant candy canes gummies chocolate tart cheesecake. Gummies chupa chups candy canes tiramisu carrot cake gummi bears tart. I love toffee powder. Pie cake I love cupcake oat cake tootsie roll chocolate bar I love.</p>
</div>
<p><strong>Photography by</strong> <a href="http:mariodesigns.co.uk/" target="blank">Mario Duarte</a></p>
<nav class="social">
<li><strong>Follow me on: </strong></li>
<li><a href="https://dribbble.com/MDesignsuk" target="_blank"><i class="fa fa-dribbble" aria-hidden="true"></i></a></li>
<li><a href="https://www.behance.net/mdesignsuk" target="_blank"><i class="fa fa-behance" aria-hidden="true"></i></a></li>
<li><a href="http://codepen.io/MarioDesigns/" target="_blank"><i class="fa fa-codepen" aria-hidden="true"></i></a></li>
<li><a href="https://bitbucket.org/Mario_Duarte/" target="_blank"><i class="fa fa-bitbucket" aria-hidden="true"></i></a></li>
<li><a href="https://github.com/Mario-Duarte" target="_blank"><i class="fa fa-github" aria-hidden="true"></i></a></li>
<li><a href="https://twitter.com/MDesignsuk" target="_blank"><i class="fa fa-twitter" aria-hidden="true"></i></a></li>
<li><a href="https://www.instagram.com/m.duarte_/" target="_blank"><i class="fa fa-instagram" aria-hidden="true"></i></a></li>
<li><a href="https://www.facebook.com/mariodesigns/" target="_blank"><i class="fa fa-facebook-official" aria-hidden="true"></i></a></li>
</nav>
</div>
</div>
$maxWidth: 960px;
$minTablet: 767px;
@mixin media($size) {
@if $size == 'tabletUpwards' {@media screen and ( min-width : $minTablet ) { @content; }}
}
body {
position: relative;
background-color: #DDDDDD;
font-family: 'helvetica', sans-serif;
font-weight: lighter;
font-size: 14px;
color: #555;
margin: 0;
padding: 0;
min-width: 320px;
}
h1 {
text-transform: uppercase;
color: #333;
}
h3 {
font-weight: lighter;
color: #555555;
}
a {
position: relative;
color: #a8244f;
text-decoration: none;
&:before {
content: "";
height: 2px;
position: absolute;
bottom: -5px;
left: 0;
right: 0;
background-color: darken(#a8244f, 10%);
transform: rotateY(90deg);
transition: transform 0.2s ease-in-out;
}
&:hover {
color: darken(#a8244f, 10%);
text-decoration: none;
&:before {
transform: rotateY(0deg);
}
}
}
.split {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-between;
align-items: strech;
p {
flex-basis: 100%;
@include media('tabletUpwards') {
flex-basis: 48%;
}
}
}
nav.social {
display: inline-block;
padding: 0;
margin-bottom: 18px;
li {
list-style: none;
float: left;
a {
padding: 5px;
}
&:first-child a {
padding-left: 0;
}
}
}
.container {
position: relative;
width: 100%;
margin: 50px 0;
.inner {
position: relative;
width: 100%;
max-width: $maxWidth;
margin: 0 auto;
overflow: hidden;
box-sizing: border-box;
padding: 20px 30px;
background-color: #EEE;
}
}
.comparison-slider-wrapper {
position: relative;
width: 100%;
margin: 20px 0;
background-color: white;
.comparison-slider {
position: relative;
width: 100%;
margin: 0;
border: 5px white solid;
box-sizing: border-box;
> img {
width: 100%;
height: auto;
display: block;
}
.overlay {
display: none;
position: absolute;
width: 250px;
bottom: 20px;
right: 20px;
background-color: rgba(0, 0, 0, 0.4);
padding: 10px;
box-sizing: border-box;
color: #DDD;
text-align: right;
@include media('tabletUpwards') {
display: block;
}
}
.resize {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 50%;
overflow: hidden;
> img {
display: block;
}
.overlay {
right: auto;
left: 20px;
text-align: left;
}
}
.divider {
position: absolute;
width: 2px;
height: 100%;
background-color: rgba(256, 256, 256, 0.2);
left: 50%;
top: 0;
bottom: 0;
margin-left: -1px;
cursor: ew-resize;
&:before {
content: "";
position: absolute;
width: 20px;
height: 20px;
left: -9px;
top: 50%;
margin-top: -10px;
background-color: white;
transform: rotate(45deg);
transition: all 0.1s ease-in-out;
}
&:after {
content: "";
position: absolute;
width: 12px;
height: 12px;
left: -5px;
top: 50%;
margin-top: -6px;
background-color: white;
transform: rotate(45deg);
transition: all 0.1s ease-in-out;
}
&.draggable{
&:before {
width: 30px;
height: 30px;
left: -14px;
margin-top: -15px;
}
&:after {
width: 20px;
height: 20px;
left: -9px;
margin-top: -10px;
background-color: #555;
}
}
}
}
.caption {
position: relative;
width: 100%;
padding: 10px;
box-sizing: border-box;
font-size: 12px;
font-style: italic;
}
}
$(document).ready(function() {
// If the comparison slider is present on the page lets initialise it, this is good you will include this in the main js to prevent the code from running when not needed
if ($(".comparison-slider")[0]) {
let compSlider = $(".comparison-slider");
//let's loop through the sliders and initialise each of them
compSlider.each(function() {
let compSliderWidth = $(this).width() + "px";
$(this).find(".resize img").css({ width: compSliderWidth });
drags($(this).find(".divider"), $(this).find(".resize"), $(this));
});
//if the user resizes the windows lets update our variables and resize our images
$(window).on("resize", function() {
let compSliderWidth = compSlider.width() + "px";
compSlider.find(".resize img").css({ width: compSliderWidth });
});
}
});
// This is where all the magic happens
// This is a modified version of the pen from Ege Görgülü - https://codepen.io/bamf/pen/jEpxOX - and you should check it out too.
function drags(dragElement, resizeElement, container) {
// This creates a variable that detects if the user is using touch input insted of the mouse.
let touched = false;
window.addEventListener('touchstart', function() {
touched = true;
});
window.addEventListener('touchend', function() {
touched = false;
});
// clicp the image and move the slider on interaction with the mouse or the touch input
dragElement.on("mousedown touchstart", function(e) {
//add classes to the emelents - good for css animations if you need it to
dragElement.addClass("draggable");
resizeElement.addClass("resizable");
//create vars
let startX = e.pageX ? e.pageX : e.originalEvent.touches[0].pageX;
let dragWidth = dragElement.outerWidth();
let posX = dragElement.offset().left + dragWidth - startX;
let containerOffset = container.offset().left;
let containerWidth = container.outerWidth();
let minLeft = containerOffset + 10;
let maxLeft = containerOffset + containerWidth - dragWidth - 10;
//add event listner on the divider emelent
dragElement.parents().on("mousemove touchmove", function(e) {
// if the user is not using touch input let do preventDefault to prevent the user from slecting the images as he moves the silder arround.
if ( touched === false ) {
e.preventDefault();
}
let moveX = e.pageX ? e.pageX : e.originalEvent.touches[0].pageX;
let leftValue = moveX + posX - dragWidth;
// stop the divider from going over the limits of the container
if (leftValue < minLeft) {
leftValue = minLeft;
} else if (leftValue > maxLeft) {
leftValue = maxLeft;
}
let widthValue = (leftValue + dragWidth / 2 - containerOffset) * 100 / containerWidth + "%";
$(".draggable").css("left", widthValue).on("mouseup touchend touchcancel", function() {
$(this).removeClass("draggable");
resizeElement.removeClass("resizable");
});
$(".resizable").css("width", widthValue);
}).on("mouseup touchend touchcancel", function() {
dragElement.removeClass("draggable");
resizeElement.removeClass("resizable");
});
}).on("mouseup touchend touchcancel", function(e) {
// stop clicping the image and move the slider
dragElement.removeClass("draggable");
resizeElement.removeClass("resizable");
});
}
Also see: Tab Triggers