跨平台响应滑块插件 Nineslider.js

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

简介

Nineslider.js是一个跨平台、响应迅速、自动和交叉衰落的滑块/转盘/幻灯片库,与jQuery、Vanilla JavaScript、React和Vue.js兼容。

它提供了一种引人入胜的互动方式,可以在博客、电子商务网站或网络应用程序上显示图像、视频和其他内容。

如何使用它:

1.下载Nineslider.js并将其导入到您的项目中。

  1. <!-- Vanilla JS -->
  2. <script src="/path/to/nineslider.js"></script>
  3.  
  4. <!-- jQuery -->
  5. <script src="/path/to/cdn/jquery.min.js"></script>
  6. <script src="/path/to/jquery.nineslider.js"></script>
  7.  
  8. <!-- Vue 2 -->
  9. <script src="/path/to/cdn/vue.min.js"></script>
  10. <script src="/path/to/vue.nineslider.js"></script>

2.将您的内容添加到滑块中。

  1. <!-- Vanilla JS & jQuery -->
  2. <ul id="demo">
  3. <li>
  4. <a href="#">
  5. <img src="1.jpg" />
  6. <div class="caption">
  7. This is a caption.
  8. </div>
  9. </a>
  10. </li>
  11. <li>
  12. <img src="2.jpg" />
  13. <div class="caption">
  14. This is another caption with <a href="#">a link</a>
  15. </div>
  16. </li>
  17. <li>
  18. <img src="3.jpg" />
  19. </li>
  20. <li>
  21. <img src="4.jpg" />
  22. </li>
  23. </ul>
  1. <!-- Vue -->
  2. <div id="demo"></div>
  1. // Vue
  2. var data = [
  3. {
  4. image: "1.jpg",
  5. link: "#",
  6. caption: 'This is a caption'
  7. },
  8. {
  9. image: "2.jpg",
  10. link: "",
  11. caption: 'This is another caption with <a href="#">a link</a>'
  12. },
  13. {
  14. image: "3.jpg",
  15. link: "",
  16. caption: null
  17. },
  18. {
  19. image: "4.jpg",
  20. link: "",
  21. caption: null
  22. }
  23. ];
  24.  
  25. var eventChannel = new Vue();
  26. var template = `
  27. <div class="nbs-nineslider-container">
  28. <template v-if="items.length > 0">
  29. <ul class="nbs-nineslider-ul" @mouseover="mouseOver" @mouseout="mouseOut" v-show="items.length >= 1">
  30. <li v-for="(item, index) in items" class="nbs-nineslider-item" :ref="'nbs-nineslider-index-' + index">
  31. <template v-if="item.link">
  32. <a :href="item.link">
  33. <img :src="item.image" />
  34. <div v-html="item.caption" class="caption" v-if="item.caption"></div>
  35. </a>
  36. </template>
  37. <template v-else>
  38. <img :src="item.image" />
  39. <div v-html="item.caption" class="caption" v-if="item.caption"></div>
  40. </template>
  41. </li>
  42. </ul>
  43. <div class="nbs-nineslider-nav-left" @click="navigate(true)" v-show="items.length > 1"></div>
  44. <div class="nbs-nineslider-nav-right" @click="navigate(false)" v-show="items.length > 1"></div>
  45. <ul class="nbs-nineslider-paging" v-show="items.length > 1">
  46. <li v-for="(item, index) in items" @click="navigateTo(index)" :class="{ active: index == currentIndex }"></li>
  47. </ul>
  48. </template>
  49. <template v-else>
  50. <p>There are no items to show</p>
  51. </template>
  52. </div>
  53. `;

3.初始化滑块。

  1. // Vanilla JavaScript
  2. window.addEventListener("load", function(){
  3. nineslider(document.getElementById("demo"), {
  4. // options here
  5. });
  6. });
  7.  
  8. // jQuery
  9. $(function(){
  10. $('#demo').nineslider({
  11. // options here
  12. });
  13. });
  14.  
  15. // Vue
  16. nineslider("#demo", {
  17. // options here
  18. loaded: function(){
  19. var self = this;
  20. eventChannel.$on("myCustomExternalNav", function (isReverse) {
  21. self.navigate(isReverse);
  22. })
  23. }
  24. }, data, template);
  25. // Vue.js omponent to control the slider externally
  26. new Vue({
  27. el: "#externalControls",
  28. methods:{
  29. left: function(){
  30. eventChannel.$emit("myCustomExternalNav", true);
  31. },
  32. right: function(){
  33. eventChannel.$emit("myCustomExternalNav", false)
  34. }
  35. }
  36. })

4.把你自己的风格运用到Nineslider上。

  1. .nbs-nineslider-container {
  2. position:relative;
  3. max-width:100%;
  4. text-align: center;
  5. }
  6.  
  7. .nbs-nineslider-ul {
  8. position:relative;
  9. margin: 0;
  10. padding: 0;
  11. list-style-type:none;
  12. }
  13.  
  14. .nbs-nineslider-ul > li {
  15. position: relative;
  16. z-index: 0;
  17. top: 0px;
  18. left: 0px;
  19. display: none; /* initialized via slider */
  20. width: 100%;
  21. }
  22.  
  23. .nbs-nineslider-ul > li .caption {
  24. position: absolute;
  25. bottom: 0;
  26. padding: 5px;
  27. box-sizing: border-box;
  28. z-index: 3;
  29. background: rgba(0,0,0,0.5);
  30. color: #fff;
  31. width: 100%;
  32. text-align: left;
  33. }
  34.  
  35. .nbs-nineslider-ul > li img {
  36. max-width: 100%;
  37. width: 100%;
  38. vertical-align: middle;
  39. }
  40.  
  41. /*** Navigation ***/
  42.  
  43. .nbs-nineslider-nav-left,
  44. .nbs-nineslider-nav-right {
  45. padding:5px 10px;
  46. border-radius:15px;
  47. -moz-border-radius:15px;
  48. -webkit-border-radius:15px;
  49. position: absolute;
  50. cursor: pointer;
  51. top: 50%;
  52. transform: translateY(-100%);
  53. z-index: 4;
  54. background: rgba(0,0,0,0.5);
  55. color: #fff;
  56. }
  57.  
  58. .nbs-nineslider-nav-left {
  59. left: 10px;
  60. }
  61.  
  62. .nbs-nineslider-nav-left:before {
  63. content: "<"
  64. }
  65.  
  66. .nbs-nineslider-nav-left.disabled {
  67. opacity: 0.4;
  68. }
  69.  
  70. .nbs-nineslider-nav-right {
  71. right: 5px;
  72. }
  73.  
  74. .nbs-nineslider-nav-right:before {
  75. content: ">"
  76. }
  77.  
  78. /*** Paging ***/
  79.  
  80. .nbs-nineslider-paging {
  81. position: relative;
  82. margin: 0 auto;
  83. padding: 0;
  84. text-align: center;
  85. z-index: 3;
  86. }
  87.  
  88. .nbs-nineslider-paging li {
  89. margin: 0 5px;
  90. display: inline-block;
  91. width: 10px;
  92. height: 10px;
  93. background: #000;
  94. border: 1px solid #ccc;
  95. cursor: pointer;
  96. }
  97.  
  98. .nbs-nineslider-paging li.active {
  99. background: #fff;
  100. }

5.可用选项和回调。

  1. <!-- Vanilla JS -->
  2. <script src="/path/to/nineslider.js"></script>
  3.  
  4. <!-- jQuery -->
  5. <script src="/path/to/cdn/jquery.min.js"></script>
  6. <script src="/path/to/jquery.nineslider.js"></script>
  7.  
  8. <!-- Vue 2 -->
  9. <script src="/path/to/cdn/vue.min.js"></script>
  10. <script src="/path/to/vue.nineslider.js"></script>

预览截图