Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

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.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

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.

+ add another resource

Packages

Add Packages

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.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <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>
  
  
  
  
  
  
  
  
              
            
!

CSS

              
                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; }

              
            
!

JS

              
                (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});
}


              
            
!
999px

Console