In this tutorial I am going to show you how to create simple text slider / rotator with CSS, Jquery without plugin. It is helpful for adding auto rotational testimonial on your website or you can slide any text based div you want with auto rotation and custom rotation speed and timer along with next previous buttons.
Create Simple Text Slider / Rotator With CSS, Jquery
HTML
Markup html for text slider in unordered list, In the below example under div container 3 li has been created with some quotes with author name and we need to auto slide these quotes after page load.
CSS
Add some stylesheet on page for styling your text slider.
JS
Finally write jquery code to make you text slider movable with custom speed, In below code you can set slider speed as per your need.
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous">script> <script> $(document).ready(function () { //rotation speed and timer var speed = 3000; var run = setInterval(rotate, speed); var slides = $('.slide'); var container = $('#slides ul'); var elm = container.find(':first-child').prop("tagName"); var item_width = container.width(); var previous = 'prev'; //id of previous button var next = 'next'; //id of next button slides.width(item_width); //set the slides to the correct pixel width container.parent().width(item_width); container.width(slides.length * item_width); //set the slides container to the correct total width container.find(elm + ':first').before(container.find(elm + ':last')); resetSlides(); //if user clicked on prev button $('#buttons a').click(function (e) { //slide the item if (container.is(':animated')) { return false; } if (e.target.id == previous) { container.stop().animate({ 'left': 0 }, 1500, function () { container.find(elm + ':first').before(container.find(elm + ':last')); resetSlides(); }); } if (e.target.id == next) { container.stop().animate({ 'left': item_width * -2 }, 1500, function () { container.find(elm + ':last').after(container.find(elm + ':first')); resetSlides(); }); } //cancel the link behavior return false; }); //if mouse hover, pause the auto rotation, otherwise rotate it container.parent().mouseenter(function () { clearInterval(run); }).mouseleave(function () { run = setInterval(rotate, speed); }); function resetSlides() { //and adjust the container so current is in the frame container.css({ 'left': -1 * item_width }); } }); //a simple function to click next link //a timer will call this function, and the rotation will begin function rotate() { $('#next').click(); } script> |
See live demo and download source code.
The above code written by Ty Stelmach, you can follow their official repository for future updates.