带jQuery 基本连续图像旋转木马

  • 源码大小:5.32KB
  • 所需积分:1积分
  • 源码编号:19JP-3619
  • 浏览次数:1061次
  • 最后更新:2023年07月03日
  • 所属栏目:滑块
本站默认解压密码:19jp.com 或 19jp_com

简介

连续图像转盘(又名滑块)是在您的网站上展示多个图像、内容等的好方法。

创建滑块是一项相当简单的任务。我们今天将使用jQuery创建一个可以处理您向它抛出的任何数量的图像的查询。

如何使用它:

1.在滑块中插入图像列表。

  1. <div id="slider">
  2. <ul>
  3. <li><img src="1.jpg" alt="Image Alt" /></li>
  4. <li><img src="2.jpg" alt="Image Alt" /></li>
  5. <li><img src="3.jpg" alt="Image Alt" /></li>
  6. <li><img src="4.jpg" alt="Image Alt" /></li>
  7. <li><img src="5.jpg" alt="Image Alt" /></li>
  8. </ul>
  9. </div>

2.将下一个/上一个控制按钮添加到滑块中。

  1. <p id="links">
  2. <a href="#" id="previous">Prev</a>
  3. <a href="#" id="next">Next</a>
  4. </p>

3.在文档中加载所需的jQuery库和jQuery轻松插件(用于轻松功能)。

  1. <script src="/path/to/cdn/jquery.min.js" defer></script>
  2. <script src="/path/to/jquery.easing.min.js" defer></script>

4.滑块和控件的主要CSS样式。

  1. /* Core Styles */
  2. #slider {
  3. width:400px;
  4. overflow:hidden;
  5. border: 10px solid #4C4C4C;
  6. height:266px;
  7. position:relative;
  8. margin:auto;
  9. }
  10.  
  11. #slider ul {
  12. list-style-type: none;
  13. margin: 0;
  14. padding: 0;
  15. position: absolute;
  16. top: 0;
  17. left: 0;
  18. display: flex;
  19. }
  20.  
  21. #slider ul li img {
  22. display: block;
  23. }
  24.  
  25. /* Control Styles */
  26. #links {
  27. width:420px;
  28. display: flex;
  29. margin: 0 auto;
  30. }
  31.  
  32. #links a {
  33. display:block;
  34. width:210px;
  35. background:#6C0204;
  36. height:50px;
  37. line-height:50px;
  38. color:#D5D5D5;
  39. text-decoration:none;
  40. text-align:center;
  41. }
  42.  
  43. #links a:hover {
  44. background:#A51D1F;
  45. color:#fff;
  46. }

5.用于激活滑块的核心JavaScript(jQuery脚本)。

  1. $(window).on('load', function() {
  2. "use strict";
  3. const imageCount = $('#slider ul li').length;
  4. const imageWidth = $('#slider ul li img').first().width();
  5. const totalWidth = (imageWidth * imageCount) + 'px';
  6. let leftPosition = 0;
  7. let counter = 0;
  8. $('#slider ul').css('width', totalWidth);
  9.  
  10. // next button
  11. $('#next').click(function() {
  12. counter++;
  13. if (counter === imageCount) {
  14. $('#slider ul').clone().appendTo('#slider');
  15. $('#slider ul').last().css('left', imageWidth + 'px');
  16.  
  17. leftPosition = `-${totalWidth}`;
  18.  
  19. $('#slider ul').last().animate({
  20. left: 0
  21. }, 700, 'easeInQuad');
  22. $('#slider ul').first().animate({
  23. left: leftPosition
  24. }, 700, 'easeInQuad', function() {
  25. $('#slider ul').first().remove();
  26. });
  27. counter = 0;
  28. } else {
  29. leftPosition = `-${counter * imageWidth}px`;
  30. $('#slider ul').animate({
  31. left: leftPosition
  32. }, 700, 'easeInQuad');
  33. }
  34. });
  35.  
  36. // previous button
  37. $('#previous').click(function() {
  38. counter--;
  39. if (counter < 0) {
  40. counter = imageCount - 1;
  41. $('#slider ul').clone().appendTo('#slider');
  42. $('#slider ul').last().css('left', `-${totalWidth}`);
  43. leftPosition = `-${counter * imageWidth}px`;
  44. $('#slider ul').last().animate({
  45. left: leftPosition
  46. }, 700, 'easeInQuad');
  47. $('#slider ul').first().animate({
  48. left: imageWidth + 'px'
  49. }, 700, 'easeInQuad', function() {
  50. $('#slider ul').first().remove();
  51. });
  52. } else {
  53. leftPosition = `-${counter * imageWidth}px`;
  54. $('#slider ul').animate({
  55. left: leftPosition
  56. }, 700, 'easeInQuad');
  57. }
  58. });
  59. });

预览截图