• vue2踩坑之项目:Swiper轮播图使用


    首先安装swiper插件

    npm i swiper@5

    安装出现错误:npm ERR

    1. npm ERR! code ERESOLVE
    2. npm ERR! ERESOLVE could not resolve
    3. npm ERR!
    4. npm ERR! While resolving: @vue/eslint-config-standard@6.1.0
    5. npm ERR! Found: eslint-plugin-vue@8.7.1
    6. npm ERR! node_modules/eslint-plugin-vue
    7. npm ERR! dev eslint-plugin-vue@"^8.0.3" from the root project
    8. npm ERR!
    9. npm ERR! Could not resolve dependency:
    10. npm ERR! peer eslint-plugin-vue@"^7.0.0" from @vue/eslint-config-standard@6.1.0
    11. npm ERR! node_modules/@vue/eslint-config-standard
    12. npm ERR! dev @vue/eslint-config-standard@"^6.1.0" from the root project
    13. npm ERR!
    14. npm ERR! Conflicting peer dependency: eslint-plugin-vue@7.20.0
    15. npm ERR! node_modules/eslint-plugin-vue
    16. npm ERR! peer eslint-plugin-vue@"^7.0.0" from @vue/eslint-config-standard@6.1.0
    17. npm ERR! node_modules/@vue/eslint-config-standard
    18. npm ERR! dev @vue/eslint-config-standard@"^6.1.0" from the root project
    19. npm ERR!
    20. npm ERR! Fix the upstream dependency conflict, or retry
    21. npm ERR! this command with --force, or --legacy-peer-deps
    22. npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
    23. npm ERR!
    24. npm ERR! See C:\Users\jing\AppData\Local\npm-cache\eresolve-report.txt for a full report.
    25. npm ERR! A complete log of this run can be found in:
    26. npm ERR! C:\Users\jing\AppData\Local\npm-cache\_logs\2023-03-22T08_20_28_486Z-debug-0.log

    解决方法:npm 版本太高,切换一下就好了

    如下代码 运行后再重新 npm i

    npm install npm@6.14.15 -g

    引入Swiper

    // 可以直接在组件里引入这个文件

    //导入js

    import Swiper from "swiper";

    //引入css

    import "swiper/css/swiper.min.css";

    代码:

    1. //html结构
    2. //js
    3. //mounted里面调用
    4. export default {
    5. components: {
    6. },
    7. setup() {
    8. return {
    9. // modules: [Pagination],
    10. };
    11. },
    12. methods: {
    13. },
    14. mounted() {
    15. var Myswiper = new Swiper(".swiper-container", {
    16. loop: true, // 循环模式选项
    17. slidesPerView: 3,
    18. centeredSlides: true,
    19. centeredSlidesBounds: true,
    20. // 自动轮播
    21. autoplay: {
    22. delay: 1300,
    23. stopOnLastSlide: false,
    24. disableOnInteraction: true,
    25. waitForTransition: false,
    26. pauseOnMouseEnter: true, // 鼠标悬停暂停轮播
    27. },
    28. // 翻转
    29. effect: 'coverflow',
    30. slidesPerView: 3,
    31. centeredSlides: true,
    32. coverflowEffect: {
    33. rotate: 30,
    34. stretch: 10,
    35. depth: 60,
    36. modifier: 2,
    37. slideShadows: true
    38. },
    39. // 如果需要分页器
    40. pagination: {
    41. el: ".swiper-pagination",
    42. clickable: true,
    43. },
    44. // 下一页,上一页
    45. navigation: {
    46. nextEl: '.swiper-button-next',
    47. prevEl: '.swiper-button-prev',
    48. },
    49. // 滚动条
    50. scrollbar: {
    51. el: ".swiper-scrollbar",
    52. hide: true,
    53. draggable: true, //是否可拖动条
    54. snapOnRelease: true, //false释放滚动条时slide不会自动贴合
    55. dragSize: 500,
    56. },
    57. });
    58. /*鼠标移入停止轮播,鼠标离开 继续轮播*/
    59. var container = document.getElementById('swiperContainer');
    60. container.onmouseover = function () {
    61. Myswiper.autoplay.stop();
    62. };
    63. container.onmouseout = function () {
    64. Myswiper.autoplay.start();
    65. }
    66. },
    67. //css
    68. <style scoped lang="scss">
    69. .swiper-container {
    70. position: relative;
    71. display: inline-block;
    72. width: 100%;
    73. height: 24rem;
    74. overflow: hidden;
    75. background-color: #ECECEC;
    76. --swiper-pagination-color: #ffbb00; // 分页器颜色
    77. /* 设置Swiper风格 */
    78. --swiper-theme-color: #ff6600;
    79. /* 单独设置按钮颜色 */
    80. --swiper-navigation-color: #00aaff;
    81. /* 设置按钮大小 */
    82. --swiper-navigation-size: 30px;
    83. h1 {
    84. color: #fff;
    85. position: relative;
    86. top: 40%;
    87. }
    88. .swiper-slide {
    89. img {
    90. width: 100%;
    91. height: 100%;
    92. }
    93. .bannerTitle {
    94. line-height: 2rem;
    95. width: 100%;
    96. position: absolute;
    97. height: 2rem;
    98. bottom: 0px;
    99. left: 10px;
    100. background: rgba(0, 0, 0, 0.5);
    101. color: rgba(255, 255, 255, 0.9);
    102. z-index: 9999;
    103. .titleC {
    104. width: 26rem;
    105. overflow: hidden;
    106. text-overflow: ellipsis;
    107. white-space: nowrap;
    108. }
    109. }
    110. }
    111. }
    112. style>

    上一篇文章, 

    vue3+elementPlus:el-select选择器里添加按钮button_意初的博客-CSDN博客vue3+elementPlus:el-select选择器里添加按钮button,在el-select的option后面添加buttonhttps://blog.csdn.net/weixin_43928112/article/details/133681154

  • 相关阅读:
    [ Linux ] 动静态库 手把手教你写一个自己的库
    基于微信小程序的智能垃圾分类回收系统,附源码、教程
    了解防抖和节流:提升前端交互体验的实用策略
    MySQL的故事——查询性能优化
    短视频消重去重九种方法,组合使用原创度更高,各平台轻松过原创
    Node直接执行ts文件
    敏感信息防泄漏:透明加密与通信内容安全策略深度解析
    java字符串储存底层原理
    实习日常的点点滴滴记录(Java基础知识之Java锁)------好记性不如烂笔头--慢慢积累,厚积薄发
    算法day42|背包问题
  • 原文地址:https://blog.csdn.net/weixin_43928112/article/details/133681437