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="header">
<div id="nav">
<ul>
<li><a href="#" onclick="scrollTo('#level1')">Level 1</a></li>
<li><a href="#" onclick="scrollTo('#level2')">Level 2</a></li>
<li><a href="#"onclick="scrollTo('#level3')">Level 3</a></li>
<li><a href="#" onclick="scrollTo('#level4')">Level 4</a></li>
<li><a href="#" onclick="scrollTo('#level5')">Level 5</a></li>
<li><a href="#" onclick="scrollTo('#level2Room1')">Level 2 Room 1</a></li>
<li><a href="#" onclick="scrollTo('#level2Room2')">Level 2 Room 2</a></li>
</ul>
</div>
</div>
<div id="contentwrapper">
<div id="content">
<div id="level1" name="level-1">level 1 page</div>
<div id="level2" name="level-2">level 2 page - more pages to the right
<div id="level2Room1" name="level-2Room1">Level 2 - Room 1</div>
<div id="level2Room2" name="level-2Room2">Level 2 - Room 2</div>
</div>
<div id="level3" name="level-3">level 3 page</div>
<div id="level4" name="level-4">level 4 page</div>
<div id="level5" name="level-5">level 5 page</div>
</div>
</div>
body {
background-color: #ffffff;
font-family: Arial, Helvetica, sans-serif;
width: 800px;
margin: 0 auto; }
#header {
position: fixed;
top: 0px;
left: 50%;
margin-left: -400px;
background: aquamarine;
height: auto;
width: 800px;
z-index: 99999; }
::-webkit-scrollbar {
display: none; }
#contentwrapper {
margin: 0 auto; }
#content {
margin-top: 10px;
z-index: 0; }
#content {
height: 600px;
overflow: hidden; }
#nav ul {
list-style: none;
vertical-align: top; }
#nav ul li {
display: inline;
margin-right: 20px; }
#nav ul li ul {
position: relative;
visibility: hidden;
list-style: none; }
#levels, #level1, #level2, #level3, #level4, #level5 {
height: 600px;
width: 800px;
padding: 70px 20px 20px 20px;
box-sizing: border-box;
position: static; }
#level1 {
background-color: #dddddd; }
#level2 {
background-color: #c3c3c3; }
#level3 {
background-color: peachpuff; }
#level4 {
background-color: lightblue; }
#level5 {
background-color: #ffeedd; }
#rooms, #level2Room1, #level2Room2 {
height: 600px;
width: 800px;
padding: 70px 20px 20px 20px;
box-sizing: border-box;
position: relative; }
#level2Room1 {
background-color: lightblue;
left: 800px;
top: -90px; }
#level2Room2 {
background-color: #a3a3a3;
left: 1610px;
top: -690px; }
(function($) {
$.belowthefold = function(element, settings) {
var fold = $(window).height() + $(window).scrollTop();
return fold <= $(element).offset().top - settings.threshold;
};
$.abovethetop = function(element, settings) {
var top = $(window).scrollTop();
return top >= $(element).offset().top + $(element).height() - settings.threshold;
};
$.rightofscreen = function(element, settings) {
var fold = $(window).width() + $(window).scrollLeft();
return fold <= $(element).offset().left - settings.threshold;
};
$.leftofscreen = function(element, settings) {
var left = $(window).scrollLeft();
return left >= $(element).offset().left + $(element).width() - settings.threshold;
};
$.inviewport = function(element, settings) {
return !$.rightofscreen(element, settings) && !$.leftofscreen(element, settings) && !$.belowthefold(element, settings) && !$.abovethetop(element, settings);
};
$.extend($.expr[':'], {
"below-the-fold": function(a, i, m) {
return $.belowthefold(a, {threshold : 0});
},
"above-the-top": function(a, i, m) {
return $.abovethetop(a, {threshold : 0});
},
"left-of-screen": function(a, i, m) {
return $.leftofscreen(a, {threshold : 0});
},
"right-of-screen": function(a, i, m) {
return $.rightofscreen(a, {threshold : 0});
},
"in-viewport": function(a, i, m) {
return $.inviewport(a, {threshold : 0});
}
});
})(jQuery);
/* Adopted from this example https://codepen.io/GreenSock/pen/gnoDc */
var content = document.getElementById("content");
var maxScrollY = content.scrollHeight - content.offsetHeight;
var maxScrollX = content.scrollWidth - content.offsetWidth;
var floors = 5;
var rooms = 3;
var draggableInstance = '';
setDraggableDirection();
//if the user flicks/spins/drags with momentum, a tween is created, but if the user interacts again before the tween is
// done, we must kill that tweens (so as not to fight with the user). This method kills any tweens of the knob or the content's scrollProxy.
function killTweens() {
var myVar = this.target;
var i = 0;
TweenLite.killTweensOf([content, draggableInstance.scrollProxy]);
}
content.addEventListener("mousewheel", killTweens);
content.addEventListener("DOMMouseScroll", killTweens);
// Dynamically set the bounds of draggable for the current using a viewport plugin
//$(":in-viewport")
//$(":below-the-fold")
//$(":above-the-top")
//$(":left-of-screen")
//$(":right-of-screen")
function setDraggableDirection() {
var divInVP = $("div[name|='level']:in-viewport").attr('name');
var divInVPObj = $("div[name|='level']:in-viewport");
var divAboveVP = $("div[name|='level']:above-the-top").attr('name');
var divBelowVP = $("div[name|='level']:below-the-fold").attr('name');
var divleftVP = $("div[name|='level']:left-of-screen").attr('name');
var divRightVP = $("div[name|='level']:right-of-screen").attr('name');
//console.log('Div in viewport: ' + divInVP);
//console.log('Div above viewport: ' + divAboveVP);
//console.log('Div below viewport: ' + divBelowVP);
//console.log('Div left of viewport: ' + divleftVP);
//console.log('Div right of viewport: ' + divRightVP);
// update the movement to horizontal or vertical or both
// first test if in a room if so then only allow x(horizontal) scrolling
var roomPattern = /Room/g;
var inRoom = '';
inRoom = divInVP.match(roomPattern);
if (typeof inRoom !== 'undefined' && inRoom !== null) {
if (inRoom.toString() == 'Room' ) {
//console.log ('In a room - dragCreate("scrollLeft")');
draggableInstance.destroy;
dragCreate("scrollLeft");
return;
}
}
// Cleanup faults viewport values for rooms on right ans left (rooms to right don't exist if not on same level)
if (typeof divRightVP != 'undefined') {
// regex pattern for stripping out level
var levelPattern = /level-[0-9]/g;
var inVPLevel = divInVP.match(levelPattern);
// strip out the room number
var levelRightOfIs = divRightVP.match(levelPattern);
// test if there is a room to the right if so then allow x,y scrolling
if (inVPLevel.toString() == levelRightOfIs.toString()) {
//console.log('Valid rooms to the right - dragCreate("scroll")');
draggableInstance.destroy;
dragCreate("scroll");
} else {
//console.log ('maxScrollY: ' + maxScrollY);
//console.log('No rooms to the right - dragCreate("scrollTop")');
draggableInstance.destroy;
dragCreate("scrollTop");
}
}
}
function dragCreate(scrollType) {
//create the X,Y content Draggable
Draggable.create(content, {
type: scrollType,
edgeResistance: 0.5,
throwProps: true,
onDragStart: killTweens,
onThrowComplete: setDraggableDirection,
lockAxis: true,
snap: {
x: function (endValue) {
var step = maxScrollX / (rooms - 1);
//console.log ('maxScrollX: ' + maxScrollX);
//console.log ('Step this much: ' + step);
//console.log ('Snapping x to: ' + Math.abs( Math.round(endValue / step) * -step ));
//console.log (' endValue: ' + endValue);
return Math.abs(Math.round(endValue / step) * -step);
},
y: function (endValue) {
var step = maxScrollY / (floors - 1);
//console.log ('maxScrollY: ' + maxScrollY);
//console.log (' y is: ' + Math.round( endValue / step) * -step);
return Math.round(endValue / step) * -step;
}
}
});
//grab the Draggable instances for the content and the knob, and store them in variables so that we can reference them in other functions very quickly.
draggableInstance = Draggable.get(content);
}
// Set up a eventHandler for each child of the content so we can adjust the bounties of the draggable object
$('#content').children().click(function () {
var whatIsI = this;
var i = 0;
});
function scrollTo(level) {
// floor or room?
// get the position of the element
var locXY = $(level).position();
var locY = locXY.top;
var locX = locXY.left;
//console.log('X: ' + locX + ' Y: ' + locY);
var myTween = TweenMax.to(content, 1, {scrollTo: {x: locX, y: locY}, ease: Power3.easeOut, onComplete:setDraggableDirection});
}
Also see: Tab Triggers