Touch Carousel - last, no gaps.

Developer
Size
4,332 Kb
Views
22,264

How do I make an touch carousel - last, no gaps.?

This is a responsive touch carousel that i have adapted from the Hammer.js example.. What is a touch carousel - last, no gaps.? How do you make a touch carousel - last, no gaps.? This script and codes were developed by Berkin on 23 September 2022, Friday.

Touch Carousel - last, no gaps. Previews

Touch Carousel - last, no gaps. - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Touch Carousel - last, no gaps.</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 class="js-carousel carousel"> <div class="js-container panel-container"> <div class="js-panel panel" data-min="true"><h2><button onclick="carousel.addPanel({ shrinkCurrent: true, min: true});">Add</button></h2></div> <div class="js-panel panel" data-min="true"><h2><button onclick="carousel.addPanel({ shrinkCurrent: true, min: true});">Add</button></h2></div> <div class="js-panel panel"><h2><button onclick="carousel.addPanel({ shrinkCurrent: true, min: true});">Add</button></h2></div> <div class="js-panel panel"><h2><button onclick="carousel.addPanel({ shrinkCurrent: true, min: true});">Add</button></h2></div> </div>
</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>

Touch Carousel - last, no gaps. - Script Codes CSS Codes

 html, body, .carousel, .panel-container, .panel { 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; } .panel-container.animate { -webkit-transition: all .3s; -moz-transition: all .3s; -o-transition: all .3s; transition: all .3s; } .panel-container { 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; } .panel-container { -webkit-box-shadow: 0 0 20px rgba(0,0,0,.2); box-shadow: 0 0 20px rgba(0,0,0,.2); position: relative; } .panel { float: left; overflow: hidden; -webkit-transform-style: preserve-3d; -webkit-transform: translate3d(0,0,0); } .panel 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); } .panel:nth-child(2n) { background: #42d692; } .panel:nth-child(3n) { background: #4986e7; } .panel:nth-child(4n) { background: #d06b64; } .panel:nth-child(5n) { background: #cd74e6; } .panel:nth-child(6n) { background: #9fe1e7; }

Touch Carousel - last, no gaps. - Script Codes JS Codes

var Carousel = (function(window, document, undefined) { function Carousel(element) { this.containerWidth = 0; this.paneGap = 0; this.$element = $(element); this.elementWidth = this.$element.width(); this.container = $(".js-container", this.$element); this.panes = $(".js-panel", this.$element); this.paneCount = 0; this.panePosition = 0; this.currentPane = 0; } Carousel.prototype.init = function() { var self = this; this.$element.hammer({ drag_lock_to_axis: true }) .on("release dragright dragleft swipeleft swiperight", self.handleHammer.bind(self)); if ( this.paneCount === 1 ) { this.panes.width(this.elementWidth); return; } this.refresh(); $(window).on("load resize orientationchange", function() { self.refresh(); }); }; Carousel.prototype.refresh = function(opts) { this.paneWidth = this.$element.width(); this.minPaneWidth = this.paneWidth/2; this.setPaneDimensions(opts); } /** * set the pane dimensions and scale the container */ Carousel.prototype.setPaneDimensions = function(opts) { var self = this; this.panes = $('.js-panel', this.$element); this.paneCount = 0 // length this.paneLength = this.panes.length; this.paneList = []; this.containerWidth = 0; this.panes.each(function() { var $this = $(this), paneIndex = $this.index(), data = $this.data(), currentPaneWidth; currentPaneWidth = data.min ? self.minPaneWidth : self.paneWidth; if ( paneIndex !== 0 ) { currentPaneWidth -= self.paneGap; } prevPaneWidth = currentPaneWidth; self.paneCount += data.min ? 1/2 : 1; var paneOpts = self.paneList.push({ index: paneIndex, min: currentPaneWidth, position: self.paneCount }); $this.width(currentPaneWidth); // set the pane width self.containerWidth += currentPaneWidth; // add pane width to container }); console.log(self.paneList); this.container.width(self.containerWidth); // set the container width this.showPane(this.currentPane, false); } Carousel.prototype.addPanel = function(opts) { var self = this, panel = $('<div class="js-panel panel" />'); panel.data('min', !!opts.min); if ( opts.shrinkCurrent === true ) { this.panes.eq(this.currentPane).data('min', true); } this.container.append(panel); this.container.removeClass("animate"); this.refresh(opts); setTimeout(function() { //self.next(); }, 750); } Carousel.prototype.getPanePosition = function(index) { var panePosition = (this.elementWidth * index) - (this.paneGap * index*2); return -panePosition * 100 / this.containerWidth; } function getOffset(value, index) { var i = value - index; return i > 0 ? 1 : -1; } /** * show pane by index * @param {Number} index */ Carousel.prototype.showPane = function( index, animate ) { // between the bounds //index = Math.max(0, Math.min(index, this.paneCount-1)); var offset = this.paneCount - index; if ( offset <= 1) { index = this.paneCount - 1; } if ( offset >= this.paneCount) { index = 0; } this.currentPane = index; this.setContainerOffset(this.getPanePosition(index), animate === false ? false : true); }; Carousel.prototype.setContainerOffset = function(percent, animate) { this.container.removeClass("animate"); if(animate) { this.container.addClass("animate"); } if(Modernizr.csstransforms3d) { this.container.css("transform", "translate3d("+ percent +"%,0,0) scale3d(1,1,1)"); } else if(Modernizr.csstransforms) { this.container.css("transform", "translate("+ percent +"%,0)"); } else { var px = ((paneWidth*paneCount) / 100) * percent; this.container.css("left", px+"px"); } } Carousel.prototype.next = function() { var i = this.isPaneMin() == true ? 1/2 : 1; this.showPane(this.currentPane+i, true); this.getMove(); if ( this.panePosition < this.paneLength ) { this.panePosition += 1; } }; Carousel.prototype.prev = function() { var i = this.isPaneMin(-1) == true ? 1/2 : 1; this.showPane(this.currentPane-i, true); if ( this.panePosition > 0 ) { this.panePosition -= 1; } }; Carousel.prototype.getMove = function() { var i = this.panePosition, len = i + 2; for (; i < len; i++) { console.log(i); } } Carousel.prototype.isPaneMin = function(offset) { console.log('panepos' + this.panePosition + "**" + (offset || 0)); var pos = this.panePosition > 0 ? this.panePosition + (offset || 0) : 0 return this.panes.eq(pos).data('min') == true ? true : false; } Carousel.prototype.handleHammer = function(ev) { var self = this; // disable browser scrolling ev.gesture.preventDefault(); switch(ev.type) { case 'dragright': case 'dragleft': // stick to the finger var paneOffset = -(100/self.paneCount)*self.currentPane; var dragOffset = ((100/self.paneWidth)*ev.gesture.deltaX) / self.paneCount; // slow down at the first and last pane if((self.currentPane == 0 && ev.gesture.direction == Hammer.DIRECTION_RIGHT) || (self.currentPane == self.paneCount-1 && ev.gesture.direction == Hammer.DIRECTION_LEFT)) { dragOffset *= 0.4; } self.setContainerOffset(dragOffset + paneOffset); break; case 'swipeleft': self.next(); ev.gesture.stopDetect(); break; case 'swiperight': self.prev(); ev.gesture.stopDetect(); break; case 'release': // more then 25% moved, navigate if(Math.abs(ev.gesture.deltaX) > self.paneWidth/4) { if(ev.gesture.direction == 'right') { self.prev(); } else { self.next(); } } else { self.showPane(self.currentPane, true); } break; } } return Carousel;
})(window, document);
var carousel = new Carousel(".js-carousel");
carousel.init();
Touch Carousel - last, no gaps. - Script Codes
Touch Carousel - last, no gaps. - Script Codes
Home Page Home
Developer Berkin
Username berkin
Uploaded September 23, 2022
Rating 3
Size 4,332 Kb
Views 22,264
Do you need developer help for Touch Carousel - last, no gaps.?

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