css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div id="menu">
  <div id="top"> <!-- "viewport" for menu -->
    <div class="swipecontainer"> <!-- Moving part for menu items -->
      <ul class="swipemenu">
        <li>One</li>
        <li>Two</li>
        <li>Three</li>
        <li>Four</li>
        <li>Five</li>
        <li>Very long Six</li>
        <li>Seven</li>
        <li>Eight</li>
      </ul>
      <div id="menumarker"></div>
    </div>
  </div>
  <div id="viewport"> <!-- "viewport" for content -->
    <div id="content">
      <div class="box one">
        <h2>#1</h2>
        <p>Hello there!<br/>
          This pen shows how to recreate Android's "sliding tabs" (from the Play store for example) in pure Javascript and HTML.</p>
      </div>
      <div class="box two">
        <h2>#2</h2>
        <p>Hello there! this is number two</p>
        <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus</p>
      </div>
      <div class="box three">
        <h2>#3</h2>
        <p>Hello there! this is number three, an example form:</p>
            <form action="#" method="post">
        <div>
             <label for="name">Text Input:</label>
             <input type="text" name="name" id="name" value="" tabindex="1" />
        </div>
        <div>
             <h4>Radio Button Choice</h4>

             <label for="radio-choice-1">Choice 1</label>
             <input type="radio" name="radio-choice-1" id="radio-choice-1" tabindex="2" value="choice-1" />

         <label for="radio-choice-2">Choice 2</label>
             <input type="radio" name="radio-choice-2" id="radio-choice-2" tabindex="3" value="choice-2" />
        </div>

      <div>
        <label for="select-choice">Select Dropdown Choice:</label>
        <select name="select-choice" id="select-choice">
          <option value="Choice 1">Choice 1</option>
          <option value="Choice 2">Choice 2</option>
          <option value="Choice 3">Choice 3</option>
        </select>
      </div>

      <div>
        <label for="textarea">Textarea:</label>
        <textarea cols="40" rows="8" name="textarea" id="textarea"></textarea>
      </div>

      <div>
          <label for="checkbox">Checkbox:</label>
        <input type="checkbox" name="checkbox" id="checkbox" />
        </div>

      <div>
          <input type="submit" value="Submit" />
        </div>
    </form>
      </div>
      <div class="box four">
        <h2>#4</h2>
        <ul>
         <li>Morbi in sem quis dui placerat ornare. Pellentesque odio nisi, euismod in, pharetra a, ultricies in, diam. Sed arcu. Cras consequat.</li>
         <li>Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus.</li>
         <li>Phasellus ultrices nulla quis nibh. Quisque a lectus. Donec consectetuer ligula vulputate sem tristique cursus. Nam nulla quam, gravida non, commodo a, sodales sit amet, nisi.</li>
         <li>Pellentesque fermentum dolor. Aliquam quam lectus, facilisis auctor, ultrices ut, elementum vulputate, nunc.</li>
        </ul>        
      </div>
      <div class="box ">
        <h2>#5</h2>
        <p>Hello there! this is number five</p>
      </div>
      <div class="box ">
        <h2>#6</h2>
        <p>Hello there! this is number six</p>
      </div>
      <div class="box ">
        <h2>#7</h2>
        <p>Hello there! this is number seven</p>
      </div>
      <div class="box ">
        <h2>#8</h2>
        <p>Final Destination! this is number eight</p>
      </div>
    </div>
  </div>
</div>
            
          
!
            
              #menu {
  width:auto;
  height:100%;
  overflow:hidden;
  background-color:#eee;
}
#top {
  height:75px;
  border-bottom:5px solid #eee;
  background-color:white;
  overflow:hidden;
  position:relative;
}

div.swipecontainer {
  position:absolute;
  top:0;
  left:0;
  height:100%;
}
ul.swipemenu {
  list-style:none;
  display:block;
  clear:both;
  padding:0;
  margin:0;
  width:9000px;
  overflow-x:hidden;
  overflow-y:auto;
  height:100%;

}
ul.swipemenu li {
  float:left;
  display:block;
  padding:25px 50px;
  margin:0;
  cursor:pointer;
  text-align:center;
  border-right:2px solid #eee; 
}

div#menumarker {
  width:200px;
  height:5px;
  display:block;
  position:absolute;
  left:0px;
  bottom:0px;
  background-color:rgba(191, 64, 64, 1);
}
div #viewport {
  position:relative;
  width:auto;
  height:900px;
  overflow:hidden;
  
}
div#content {
  width:9000px;
  height:100%;
  overflow-x:auto;
  overflow-y:hidden;
  position:absolute;
  top:0px;
  left:0px;
}
#content .box {
  height:100%;
  float:left;
  padding:20px;
  margin:0;
  margin-right:100px;
  box-shadow:0px 0px 10px #ccc;
  background:white;
}

/*optional styling thingies */
body {
  font-family:"Open Sans",sans-serif;
}
p,li,input,label,select {
  font-size:14px;
  line-height:1.5em;  
}
            
          
!
            
              var viewPort = document.getElementById('viewport');

//global variables 
boxes = $(".box");
viewPortW = $(viewPort).outerWidth(true);
boxes.width(viewPortW-40);

boxW = $(boxes[0]).outerWidth(true);
menuItems = $(".swipemenu li").length;
menuItemW = $(".swipemenu li:first").outerWidth(true);
$("#menumarker").width(menuItemW);
activeIndex = 0;

//OPTIONS
animationOptions = {duration: 500, easing:"easeInOutCubic"}; //menu animations
swipeTreshold = 150; //activates swipe

//Actually Changing tabs
function setActive(i) {
  current = $(boxes[i]);
  current.addClass("active");
  var marker = $("#menumarker");
  var scrollBoundaryRight = viewPortW*0.6; //fraction of real viewport
  var scrollBoundaryLeft = viewPortW*0.4;
  
  //check if menumarker goes out of screen (on either side)
  var dx=null;
  if((marker.offset().left+marker.width()) > scrollBoundaryRight && (i>activeIndex)) {
    dx =  (marker.offset().left+marker.width()) - scrollBoundaryRight;
  } 
  if (marker.offset().left < scrollBoundaryLeft && (i<activeIndex)) {
    dx =  marker.offset().left - scrollBoundaryLeft;
  }
  if(dx) { $(".swipecontainer").animate({left: "-="+dx+"px" }, animationOptions); }
  
  //Change content
  $("#content").animate({left: -(boxW*i) }, animationOptions); 
  
  //Update the colored box in the menu
  var menuItem = $(".swipemenu li:eq("+i+")");
  marker.animate({ left:   menuItem.position().left,
                  width:   menuItem.outerWidth()
                 }, animationOptions); 
  
  activeIndex = i;
}

//inital setting
setActive(0);

//EVENT listeners
//---------------
$(".swipemenu li").on("click",function(e){
  var i = $(".swipemenu li").index($(e.currentTarget));
  setActive(i);
});

//Hammer JS for all touch related magic
var mc = new Hammer(viewPort);
mc.add(new Hammer.Pan({ threshold: 0, pointers: 0, direction: Hammer.DIRECTION_HORIZONTAL }));

startX = $("#content").position().left;
//listen to events
mc.on("panstart panmove pancancel panend", function(ev){
  if(ev.type=="panstart"){
    startX = $("#content").position().left;
  } 
  //update content position during touch event
  else if(ev.type=="panmove") { 
    $("#content").css('left',startX + ev.deltaX+'px');
    $("#menumarker").css('left',(menuItemW*activeIndex) + (-0.2*ev.deltaX)+'px');  
  } 
  else if(ev.type=="panend" || ev.type=="pancancel") {
    if(ev.deltaX > swipeTreshold && activeIndex > 0) { //bigger than treshold, so next slide
      setActive(activeIndex-1); console.log("previous!");
    }
    else if(ev.deltaX < swipeTreshold && activeIndex < menuItems-1 ) {
      setActive(activeIndex+1); console.log("next!");
    }
    else {
      //bounce effect
      $("#content").animate({left: startX }, animationOptions); 
      $("#menumarker").animate({left: (menuItemW*activeIndex)}, animationOptions); 
    }
  }  
});
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console