Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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 class="container-fluid">
  <div class="row">
    <div class="col-sm-2">
      <button class="btn" id="left_btn">
        Shift left
      </button>
    </div>
    <div class="col-sm-8 carousel">
      <div class="carousel-inner">
        <div class="carousel-item">
          <div id="item1" class="item">
            Item 1
          </div>
        </div>
        <div class="carousel-item">
          <div id="item2" class="item">
            Item 2
          </div>
        </div>
        <div class="carousel-item">
          <div id="item3" class="item">
            Item 3
          </div>
        </div>
        <div class="carousel-item">
          <div id="item4" class="item">
            Item 4
          </div>
        </div>
        <div class="carousel-item">
          <div id="item5" class="item">
            Item 5
          </div>
        </div>
        <div class="carousel-item">
          <div id="item6" class="item">
            Item 6
          </div>
        </div>
        <div class="carousel-item">
          <div id="item7" class="item">
            Item 7
          </div>
        </div>
      </div>
    </div>
    <div class="col-sm-2" id="right_btn">
      <button class="btn">
        Shift Right
      </button>
    </div>
  </div>
</div>
<br>
<br>
<hr>
<button id="seeInner">See carousel overflow</button>
              
            
!

CSS

              
                .col-sm-2 {
  padding-left: 15px;
  padding-right: 15px;
}

.btn {
  margin-top: 30px;
  margin-bottom: 30px;
}

.item {
  height: 100px;
  border: 5px solid blue;
  background-color: pink;
}

/* ADDED CCSS */
.carousel{
  height:100px;
  overflow:hidden;
  border:1px solid black;
}
.carousel-inner{
  width:10000px;
}
.carousel-item{
  display:inline-block;
}
              
            
!

JS

              
                $(document).ready(function() {

  // Get our element count.
  var carouselItemCount = $(".carousel-item").length;
  // An index to always know the sliding container position.
  var moveIndex = 0;
  // Always have 3 elements in view.
  var maxMove = carouselItemCount-2;

  // Function to set all elements widths.
  function setWidths(){
    // Get the window witdh
    var WWidth = $(window).width();
    console.log("WWidth: "+WWidth);

    // Are we in a Small device?
    var SM = false;
    if(WWidth<768){
      SM = true;
    }
    // If yes, wewant to show 3 elements anyway,
    // so we can't rely on the .col-sm-2 width
    // because it is equal to 100% of the window.
    if(SM){
      var SM2width = WWidth/4;
    }else{
      var SM2width = $(".col-sm-2").first().outerWidth(true);
    }
    $(".carousel-item").width(SM2width);

    // Get the items widths with and without border/margin
    var carouselItemWidth = $(".carousel-item").width();
    var carouselItemOuterWidth = $(".carousel-item").outerWidth(true);

    // Set carousel item margin (remainder of caroussel-inner - 3* items);
    var carouselInnerVisibleSpaceLeft = $(".carousel-inner").parent("div").innerWidth() - (carouselItemWidth * 3);
    $(".carousel-item").css({"margin": "0 "+carouselInnerVisibleSpaceLeft/8+"px"});

    // Get the new items outer widths (we just changed it)
    var NewCarouselItemOuterWidth = $(".carousel-item").outerWidth(true);

    // Get the items border, as it causes an offset issue.
    var border = parseInt($(".item").css("border"));
    console.log("border: "+border);

    // Set animate postion (needed on resize)
    $(".carousel-inner").animate({"left":-moveIndex * (NewCarouselItemOuterWidth + border)},0);


    // ========== Movements triggered by user

    // LEFT
    $("#left_btn").off('click').on('click', function(e) {

      console.log("Click left");
      moveIndex++;
      if(moveIndex<maxMove){
        $(".carousel-inner").animate({"left":-moveIndex * (NewCarouselItemOuterWidth + border)},800);
      }else{
        console.log("full left reached");
        // Canccel that moveIndex addition.
        moveIndex--;
      }
    });

    // RIGHT
    $("#right_btn").off('click').on('click', function(e) {

      console.log("Click right");
      moveIndex--;
      if(moveIndex>=0){
        $(".carousel-inner").animate({"left":-moveIndex * (NewCarouselItemOuterWidth + border)},800);
      }else{
        console.log("full right reached");
        // Canccel that moveIndex substraction.
        moveIndex++;
      }
    });

  } // setWidths

  // Set all elements width on load and get the carousel item widths needed for movements.
  setWidths();
  // Set all elements width on resize.
  $(window).on("resize", setWidths);


  //===============================
  // DEMO BUTTONS
  $("#seeInner").on("click", function(){
    if( $(".carousel").css("overflow") == "hidden" ){
      $(".carousel").css({"overflow":"visible"});
      $(this).html("Hide carousel overflow");
    }else{
      $(".carousel").css({"overflow":"hidden"});
      $(this).html("See carousel overflow");             
    }
  });
}); // Ready
              
            
!
999px

Console