Simple carousel
How do I make an simple carousel?
What is a simple carousel? How do you make a simple carousel? This script and codes were developed by Berkin on 10 September 2022, Saturday.
Simple carousel - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>simple carousel</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <section class="carousel js-hotel-carousel"> <ul class="carousel-container"> <li class="carousel-items"><img src="http://lorempixel.com/840/460/sports" alt="description of photo 1" /></li> <li class="carousel-items"><img src="http://lorempixel.com/840/460/sports/2" alt="description of photo 2" /></li> <li class="carousel-items"><img src="http://lorempixel.com/840/460/sports/3" alt="description of photo 3" /></li> </ul> </section> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> <script src="js/index.js"></script>
</body>
</html>
Simple carousel - Script Codes CSS Codes
/* carousel { */
.carousel { width: 840px; height: 460px; position: relative; overflow: hidden;
}
.carousel-container { list-style: none; padding: 0; margin: 0; position: absolute; top: 0; left: 0; height: 100%; width: 99999px;
}
.carousel-items { float: left;
}
.carousel-next,
.carousel-prev { background: #003580; cursor: pointer; border: 1px solid #fff; color: #fff; border-radius: 5px; padding: 10px; &:hover { }
}
.carousel-next:active,
.carousel-prev:active { -webkit-box-shadow: inset 0 1px 1px 1px rgba(0, 0, 0, .3); -moz-box-shadow: inset 0 1px 1px 1px rgba(0, 0, 0, .3); box-shadow: inset 0 1px 1px 1px rgba(0, 0, 0, .3);
}
.carousel-next { position: absolute; top: 50%; right: 10px; z-index: 10;
}
.carousel-prev { position: absolute; top: 50%; left: 10px;
}
.carousel-items { position: relative;
}
.carousel-items-title { background: #000; opacity: .7; color: #fff; font-size: 24px; position: absolute; bottom: 0; left: 0; right: 0; padding: 20px;
}
/* carousel } */
Simple carousel - Script Codes JS Codes
// there are tests on the tests folder.
// the carousel has next/prev buttons and auto slide options.
// Automatic slide pauses on hover, there is a test for that in test folder.
;(function($, window, document, undefined) { 'use strict'; // define variables var pluginName = "carousel", defaultOptions = { auto: false, prevButtonText: "Prev", nextButtonText: "Next" }; /** * Carousel plugin constructor. * @constructor * @param {string} selector * @param {json} options */ function Carousel(selector, options) { this.options = $.extend({}, defaultOptions, options); this.selector = $(selector); this.container = this.selector.find('.carousel-container'); this.items = this.container.find('.carousel-items'); this.itemSize = this.items.size(); this.current = 0; if (this.options.auto) { this.start(); this.selector .on('mouseenter.carousel', $.proxy(this.stop, this)) .on('mouseleave.carousel', $.proxy(this.start, this)); } this.init(); } /** * Initiate the plugin. */ Carousel.prototype.init = function() { this.width = this.selector.width(); this.nextButton = $('<button class="carousel-next">' + this.options.nextButtonText + '</button>'); this.prevButton = $('<button class="carousel-prev">' + this.options.prevButtonText + '</button>'); this.nextButton.appendTo(this.selector); this.prevButton.appendTo(this.selector); this.nextButton.on('click.carousel', $.proxy(this.next, this)); this.prevButton.on('click.carousel', $.proxy(this.prev, this)); this._setTitles(); } /** * Set titles from img alt attributes */ Carousel.prototype._setTitles = function() { this.items.each(function() { var $this = $(this), title = $this.find('img').attr("alt"); $('<div class="carousel-items-title" />').html(title).appendTo($this); }); } /** * Go to next slide * */ Carousel.prototype.next = function() { this.current += 1; if (this.current === this.itemSize) { this.current = 0; } this.container.animate({ left: -this.current * this.width }); } /** * Go to prev slide * */ Carousel.prototype.prev = function() { if (this.current === 0) { this.current = this.itemSize; } this.current -= 1; this.container.animate({ left: -this.current * this.width }); } /** * Start auto slide * */ Carousel.prototype.start = function() { var self = this; this.intervalId = setInterval(function() { self.next(); }, this.options.auto); } /** * Stop auto slide * */ Carousel.prototype.stop = function() { clearInterval(this.intervalId); } /** * Add plugin to jQuery.prototype * * @param {json} options * @return {jQuery} chainable jQuery class */ $.fn[pluginName] = function(options) { // jQuery each returns this, so it is chainable return this.each(function() { if (!$.data(this, 'plugin_' + pluginName)) { $.data(this, 'plugin_' + pluginName, new Carousel(this, options)); } }); }
})(jQuery, window, document); window.onload = function() { $('.js-hotel-carousel').carousel({ auto: 3000 }); };
Developer | Berkin |
Username | berkin |
Uploaded | September 10, 2022 |
Rating | 3 |
Size | 3,098 Kb |
Views | 24,288 |
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 |
Touch Carousel | 3,901 Kb |
Background origin, content-box | 1,632 Kb |
IOS style alphabetic scroll | 3,488 Kb |
Tether control | 2,582 Kb |
Javascript - Selection Sort | 1,577 Kb |
CSS switch, works with swipes | 4,825 Kb |
JQuery UI datepicker with arrow pointing the input | 2,246 Kb |
Pull to Refresh | 2,528 Kb |
Big factorial | 1,567 Kb |
Hackerrank test | 1,632 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 |
Process Accordion | Devilskitchen | 31,432 Kb |
A Pen by Markku Lehmonen | SharpDal | 7,804 Kb |
Growing Root - Scroll control - CANVAS | Cjonasw | 2,342 Kb |
Button Option Group | Honchoman | 1,859 Kb |
CSS3 Snow Animation | NickyCDK | 1,695 Kb |
Multi column experiment. | Spylefkaditis | 2,805 Kb |
CSS Parent Selector | Tomhodgins | 2,143 Kb |
Boxes | H3l1um | 2,563 Kb |
CSS- UI Element States Pseudo-Classes | Tesla809 | 2,206 Kb |
CSS Heart Loaders | Nourabusoud | 2,161 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!