Tap and hold carousel with hammer.js
How do I make an tap and hold carousel with hammer.js?
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 tap and hold carousel with hammer.js? How do you make a tap and hold carousel with hammer.js? This script and codes were developed by Berkin on 10 September 2022, Saturday.
Tap and hold carousel with hammer.js - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Tap and hold carousel with hammer.js</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>Tap hold and drag...</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='http://cdn.jsdelivr.net/jquery.hammerjs/1.0.2/jquery.hammer-full.js'></script> <script src="js/index.js"></script>
</body>
</html>
Tap and hold carousel with hammer.js - Script Codes CSS Codes
html, body, #carousel, #carousel ul, #carousel li { min-height: 100%; height: 100%; padding: 0; margin: 0; position: relative;
}
#carousel ul { background: #333; -moz-box-shadow: inset 0 0 200px rgba(0, 0, 0, 0.2); -webkit-box-shadow: inset 0 0 200px rgba(0, 0, 0, 0.2); box-shadow: inset 0 0 200px rgba(0, 0, 0, 0.2);
}
#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, 0.2); box-shadow: 0 0 20px rgba(0, 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, 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;
}
.holdingAnimation li { -moz-transition: all 0.5s ease; -o-transition: all 0.5s ease; -webkit-transition: all 0.5s ease; transition: all 0.5s ease;
}
.holding li { -moz-transform: scale3d(0.95, 0.95, 0.95) !important; -ms-transform: scale3d(0.95, 0.95, 0.95) !important; -webkit-transform: scale3d(0.95, 0.95, 0.95) !important; transform: scale3d(0.95, 0.95, 0.95) !important;
}
Tap and hold carousel with hammer.js - Script Codes JS Codes
/** * 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; var transitionEnd = (function(transition) { var transEndEventNames = { 'WebkitTransition': 'webkitTransitionEnd', 'MozTransition': 'transitionend', 'OTransition': 'oTransitionEnd', 'msTransition': 'msTransitionEnd', 'transition': 'transitionend' }; return transEndEventNames[transition]; })(Modernizr.prefixed('transition')) /** * 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) { // disable browser scrolling ev.gesture.preventDefault(); console.log(ev.type); 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 *= 0.4; } setContainerOffset(drag_offset + pane_offset); break; case 'swipeleft': destroy(); self.next(); ev.gesture.stopDetect(); break; case 'swiperight': destroy(); self.prev(); ev.gesture.stopDetect(); break; case 'release': $('#debug').html('released...'); destroy(); // 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; } } function destroy() { element.hammer().off('release dragleft dragright swipeleft swiperight'); setTimeout(function() { element.removeClass('holding'); }, 500) } element.hammer().on('hold', function() { $('#debug').html('held...'); //var $currentPane = element.find('li:eq(' + current_pane + ')'); element.addClass('holding holdingAnimation'); element.hammer({ drag_lock_to_axis: true }) .on("release dragleft dragright swipeleft swiperight", handleHammer); }); } var carousel = new Carousel("#carousel"); carousel.init();
Developer | Berkin |
Username | berkin |
Uploaded | September 10, 2022 |
Rating | 3 |
Size | 4,945 Kb |
Views | 66,792 |
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!
Name | Size |
Tether control | 15,630 Kb |
Javascript - Insertion Sort | 1,501 Kb |
Hackerrank test | 1,632 Kb |
Pull to refresh - hammer.js | 4,903 Kb |
IOS style alphabetic scroll | 3,488 Kb |
Big factorial | 1,567 Kb |
Background origin, content-box | 1,632 Kb |
Touch Carousel - First One | 3,901 Kb |
Tether control | 2,582 Kb |
Javascript - Selection Sort | 1,577 Kb |
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!
Name | Username | Size |
Count up | Alanshortis | 2,391 Kb |
The Rope | Chribbe | 2,886 Kb |
SVG Playground | Roygwells | 1,834 Kb |
Custom Select Element | Agrayson | 3,616 Kb |
CMP5-Opdracht15 | SannevanGastel | 2,733 Kb |
Toggling Divs with jQuery | Yying6 | 1,967 Kb |
Webkit decolorize | Fivera | 1,675 Kb |
Jochaho Skeleton | Dhanushbadge | 1,689 Kb |
Week7 replicate | Hwcasis | 1,620 Kb |
Map Controls | Iliadraznin | 3,721 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!