Touch Carousel

Developer
Size
3,901 Kb
Views
32,384

How do I make an touch carousel?

This is a responsive touch carousel that i have adapted from the Hammer.js example. Using hardware accelerated CSS3 it supports all content types and is built right into the latest version of Bootstrap!. What is a touch carousel? How do you make a touch carousel? This script and codes were developed by Berkin on 10 September 2022, Saturday.

Touch Carousel Previews

Touch Carousel - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Touch Carousel</title> <script src="https://s.codepen.io/assets/libs/modernizr.js" type="text/javascript"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css"> <link rel='stylesheet prefetch' href='https://twitter.github.io/bootstrap/dist/css/bootstrap.css'> <link rel="stylesheet" href="css/style.css">
</head>
<body> <!--<div id="debug">Log</div>--> <div id="carousel"> <ul> <li class="pane1"><h2>Swipe...</h2></li> <li class="pane2"><h2>...or drag...</h2></li> <li class="pane3"><h2>...or swipe...</h2></li> <li class="pane4"><h2>..or drag...</h2></li> <li class="pane5"><h2>Dit is het einde!</h2></li> </ul> </div> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src='https://eightmedia.github.io/hammer.js/dist/jquery.hammer.js'></script> <script src="js/index.js"></script>
</body>
</html>

Touch Carousel - Script Codes CSS Codes

 html, body, #carousel, #carousel ul, #carousel li { min-height: 100%; height: 100%; padding: 0; margin: 0; position: relative; } #debug { position: fixed; background: #fff; padding: 5px; color: #000; top: 0; left: 0; z-index: 1337; } #carousel { background: silver; overflow: hidden; width:100%; -webkit-backface-visibility: hidden; -webkit-transform: translate3d(0,0,0) scale3d(1,1,1); -webkit-transform-style: preserve-3d; } #carousel ul.animate { -webkit-transition: all .3s; -moz-transition: all .3s; -o-transition: all .3s; transition: all .3s; } #carousel ul { transform: translate3d(0%,0,0) scale3d(1,1,1); -o-transform: translate3d(0%,0,0) scale3d(1,1,1); -ms-transform: translate3d(0%,0,0) scale3d(1,1,1); -moz-transform: translate3d(0%,0,0) scale3d(1,1,1); -webkit-transform: translate3d(0%,0,0) scale3d(1,1,1); overflow: hidden; -webkit-backface-visibility: hidden; -webkit-transform-style: preserve-3d; } #carousel ul { -webkit-box-shadow: 0 0 20px rgba(0,0,0,.2); box-shadow: 0 0 20px rgba(0,0,0,.2); position: relative; } #carousel li { float: left; overflow: hidden; -webkit-transform-style: preserve-3d; -webkit-transform: translate3d(0,0,0); } #carousel li h2 { color: #fff; font-size: 30px; text-align: center; position: absolute; top: 40%; left: 0; width: 100%; text-shadow: -1px -1px 0 rgba(0,0,0,.2); } #carousel li.pane1 { background: #42d692; } #carousel li.pane2 { background: #4986e7; } #carousel li.pane3 { background: #d06b64; } #carousel li.pane4 { background: #cd74e6; } #carousel li.pane5 { background: #9fe1e7; }

Touch Carousel - Script Codes JS Codes

 var debug_el = $("#debug"); function debug(text) { debug_el.text(text); } /** * requestAnimationFrame and cancel polyfill */ (function() { var lastTime = 0; var vendors = ['ms', 'moz', 'webkit', 'o']; for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) { window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame']; window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame'] || window[vendors[x]+'CancelRequestAnimationFrame']; } if (!window.requestAnimationFrame) window.requestAnimationFrame = function(callback, element) { var currTime = new Date().getTime(); var timeToCall = Math.max(0, 16 - (currTime - lastTime)); var id = window.setTimeout(function() { callback(currTime + timeToCall); }, timeToCall); lastTime = currTime + timeToCall; return id; }; if (!window.cancelAnimationFrame) window.cancelAnimationFrame = function(id) { clearTimeout(id); }; }()); /** * super simple carousel * animation between panes happens with css transitions */ function Carousel(element) { var self = this; element = $(element); var container = $(">ul", element); var panes = $(">ul>li", element); var pane_width = 0; var pane_count = panes.length; var current_pane = 0; /** * initial */ this.init = function() { setPaneDimensions(); $(window).on("load resize orientationchange", function() { setPaneDimensions(); //updateOffset(); }) }; /** * set the pane dimensions and scale the container */ function setPaneDimensions() { pane_width = element.width(); panes.each(function() { $(this).width(pane_width); }); container.width(pane_width*pane_count); }; /** * show pane by index * @param {Number} index */ this.showPane = function( index ) { // between the bounds index = Math.max(0, Math.min(index, pane_count-1)); current_pane = index; var offset = -((100/pane_count)*current_pane); setContainerOffset(offset, true); }; function setContainerOffset(percent, animate) { container.removeClass("animate"); if(animate) { container.addClass("animate"); } if(Modernizr.csstransforms3d) { container.css("transform", "translate3d("+ percent +"%,0,0) scale3d(1,1,1)"); } else if(Modernizr.csstransforms) { container.css("transform", "translate("+ percent +"%,0)"); } else { var px = ((pane_width*pane_count) / 100) * percent; container.css("left", px+"px"); } } this.next = function() { return this.showPane(current_pane+1, true); }; this.prev = function() { return this.showPane(current_pane-1, true); }; function handleHammer(ev) { console.log(ev); // disable browser scrolling ev.gesture.preventDefault(); switch(ev.type) { case 'dragright': case 'dragleft': // stick to the finger var pane_offset = -(100/pane_count)*current_pane; var drag_offset = ((100/pane_width)*ev.gesture.deltaX) / pane_count; // slow down at the first and last pane if((current_pane == 0 && ev.gesture.direction == Hammer.DIRECTION_RIGHT) || (current_pane == pane_count-1 && ev.gesture.direction == Hammer.DIRECTION_LEFT)) { drag_offset *= .4; } setContainerOffset(drag_offset + pane_offset); break; case 'swipeleft': self.next(); ev.gesture.stopDetect(); break; case 'swiperight': self.prev(); ev.gesture.stopDetect(); break; case 'release': // more then 50% moved, navigate if(Math.abs(ev.gesture.deltaX) > pane_width/2) { if(ev.gesture.direction == 'right') { self.prev(); } else { self.next(); } } else { self.showPane(current_pane, true); } break; } } element.hammer({ drag_lock_to_axis: true }) .on("release dragleft dragright swipeleft swiperight", handleHammer); } var carousel = new Carousel("#carousel"); carousel.init();
Touch Carousel - Script Codes
Touch Carousel - Script Codes
Home Page Home
Developer Berkin
Username berkin
Uploaded September 10, 2022
Rating 3
Size 3,901 Kb
Views 32,384
Do you need developer help for Touch Carousel?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Berkin (berkin) Script Codes
Create amazing SEO content with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!