当前位置:   article > 正文

vue实现轮播图效果_vue轮播图怎么实现

vue轮播图怎么实现

1,实现轮左右按键轮播效果,采用数组下标指定轮播图片,定义一个dindex与index下标相比较。
2,创建轮播的图片的数组
3,实现下方圆点点击轮播图片,采用动态绑定和数组的动态样式实现未点击的白色和点击的黑色
dindex与index相比较两值相不一致就是白色反之为黑色
4,实现图片定时轮播,创建定时器,将方法放入mounted生命周期中使其定时轮播,点击按钮轮播时清除定时器

5,实现鼠标移入移出左右切换图片按钮的样式切换,使用css3中的:hover的样式

一,body, HTML代码

  1. <body>
  2. <div id="box">
  3. <button id="left" @click="left">//左侧点击按钮
  4. <
  5. </button>
  6. <img id="img":src="img[dindex]" >//轮播的图片采用动态绑定渲染
  7. <button id="right" @click="right">
  8. >
  9. </button>
  10. <ul>
  11. //采用v-for循环实现圆点的动态个数渲染
  12. <li v-for="(item,index) in img.length" @click="yuan(index)">
  13. //不同图片的渲染
  14. <img :src="index===dindex?'./圆点 .png':'./圆点2.png'" alt="">
  15. </li>
  16. </ul>
  17. </div>
  18. </body

二,css样式

  1. <style>
  2. //轮播图片的位置
  3. #box{
  4. position: fixed;
  5. top: 100px;
  6. right: 150px;
  7. }
  8. #img{
  9. height: 580px;
  10. width: 1200px;
  11. }
  12. button{
  13. font-size: 50px;
  14. height: 60px;
  15. width: 100px;
  16. //按钮透明度设置
  17. opacity: 0.2;
  18. //按钮圆角设置
  19. border-top-left-radius: 20%;
  20. border-top-right-radius: 20%;
  21. border-bottom-left-radius: 20%;
  22. border-bottom-right-radius: 20%;
  23. }
  24. //鼠标移入样式
  25. button:hover{
  26. background-color: darkslategray;
  27. }
  28. #left{
  29. position: fixed;
  30. top: 350px;
  31. left: 187px;
  32. }
  33. #right{
  34. position: fixed;
  35. top: 350px;
  36. right: 150px;
  37. }
  38. ul{
  39. list-style: none;
  40. position: fixed;
  41. top: 620px;
  42. left: 700px;
  43. }
  44. li{
  45. font-size: 10px;
  46. display:inline;
  47. }
  48. li img{
  49. height: 40px;
  50. width: 40px;
  51. }
  52. </style>

三,script功能实现代码

  1. <script>
  2. new Vue({
  3. el:"#box",
  4. data:{
  5. //定义图片数组
  6. img:["./nba1.png", "./nba2.png",
  7. "./nba3.png","./nba4.png","./nba5.png","./nba.png"
  8. ],
  9. //图片下标
  10. dindex:0,
  11. //定时器设定
  12. timer:null
  13. },
  14. methods:{
  15. //右侧点击按钮
  16. right(){
  17. clearInterval(this.timer)
  18. if(this.dindex<this.img.length-1){
  19. this.dindex++
  20. }else if(this.dindex==this.img.length-1){
  21. this.dindex=0
  22. }
  23. console.log(this.dindex)
  24. },
  25. //左侧点击按钮
  26. left(){
  27. clearInterval(this.timer)
  28. if(this.dindex>0){
  29. this.dindex--
  30. }else if(this.dindex==0){
  31. this.dindex=this.img.length-1
  32. }
  33. // console.log(this.dindex)
  34. },
  35. //圆角点击实现轮播效果
  36. yuan(index){
  37. clearInterval(this.timer)
  38. this.dindex=index
  39. console.log(index,this.dindex)
  40. },
  41. //定时器设置
  42. starInterval(){
  43. clearInterval(this.timer);
  44. this.timer==setInterval(()=>{
  45. this.dindex++
  46. if(this.dindex>this.img.length-1){
  47. this.dindex=0
  48. }
  49. },3000)
  50. },
  51. },
  52. //将定时器放入mounted生命周期中
  53. mounted(){
  54. this.starInterval()
  55. }
  56. })
  57. </script>


四,整体代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <script src="./node_modules/vue/dist/vue.js"></script>
  9. </head>
  10. <style>
  11. #box{
  12. position: fixed;
  13. top: 100px;
  14. right: 150px;
  15. }
  16. #img{
  17. height: 580px;
  18. width: 1200px;
  19. }
  20. button{
  21. font-size: 50px;
  22. height: 60px;
  23. width: 100px;
  24. opacity: 0.2;
  25. border-top-left-radius: 20%;
  26. border-top-right-radius: 20%;
  27. border-bottom-left-radius: 20%;
  28. border-bottom-right-radius: 20%;
  29. }
  30. button:hover{
  31. background-color: darkslategray;
  32. }
  33. #left{
  34. position: fixed;
  35. top: 350px;
  36. left: 187px;
  37. }
  38. #right{
  39. position: fixed;
  40. top: 350px;
  41. right: 150px;
  42. }
  43. ul{
  44. list-style: none;
  45. position: fixed;
  46. top: 620px;
  47. left: 700px;
  48. }
  49. li{
  50. font-size: 10px;
  51. display:inline;
  52. }
  53. li img{
  54. height: 40px;
  55. width: 40px;
  56. }
  57. </style>
  58. <body>
  59. <div id="box">
  60. <button id="left" @click="left"><</button>
  61. <img id="img":src="img[dindex]" alt="" @mouseenter="zymInter(true)" @mouseleave="zymInter(false)">
  62. <button id="right" @click="right">></button>
  63. <ul>
  64. <li v-for="(item,index) in img.length" @click="yuan(index)"><img :src="index===dindex?'./圆点 .png':'./圆点2.png'" alt=""></li>
  65. </ul>
  66. </div>
  67. </body>
  68. <script>
  69. new Vue({
  70. el:"#box",
  71. data:{
  72. img:["./nba1.png", "./nba2.png",
  73. "./nba3.png","./nba4.png","./nba5.png","./nba.png"
  74. ],
  75. dindex:0,
  76. timer:null
  77. },
  78. methods:{
  79. right(){
  80. clearInterval(this.timer)
  81. if(this.dindex<this.img.length-1){
  82. this.dindex++
  83. }else if(this.dindex==this.img.length-1){
  84. this.dindex=0
  85. }
  86. console.log(this.dindex)
  87. },
  88. left(){
  89. clearInterval(this.timer)
  90. if(this.dindex>0){
  91. this.dindex--
  92. }else if(this.dindex==0){
  93. this.dindex=this.img.length-1
  94. }
  95. // console.log(this.dindex)
  96. },
  97. yuan(index){
  98. clearInterval(this.timer)
  99. this.dindex=index
  100. console.log(index,this.dindex)
  101. },
  102. starInterval(){
  103. clearInterval(this.timer);
  104. this.timer==setInterval(()=>{
  105. this.dindex++
  106. if(this.dindex>this.img.length-1){
  107. this.dindex=0
  108. }
  109. },3000)
  110. },
  111. },
  112. mounted(){
  113. this.starInterval()
  114. }
  115. })
  116. </script>
  117. </html>

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号