老师我想在页面中插入了两个轮播图可以怎么做?可以给我做个简单的演示吗?
是再new一个swiper对象吗???,new完以后怎么弄呢??
13
收起
正在回答
1回答
同学你好,如果页面中的两个轮播图效果不一样,那么需要重新new一个swiper对象,如果两个轮播图效果一样,就不要重新new一个swiper对象。老师这里给同学举个简单的示例:
1、两个轮播图效果一致
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | <!DOCTYPE html> < html lang = "en" > < head > < meta charset = "UTF-8" > < meta name = "viewport" content = "width=device-width, initial-scale=1.0" > < title >Document</ title > < link href = "https://cdn.bootcss.com/Swiper/4.5.1/css/swiper.min.css" rel = "stylesheet" > < script src = "https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js" ></ script > < script src = "https://cdn.bootcss.com/Swiper/4.5.1/js/swiper.min.js" ></ script > < style > html, body { position: relative; height: 100%; } body { background: #eee; font-family: Helvetica Neue, Helvetica, Arial, sans-serif; font-size: 14px; color: #000; margin: 0; padding: 0; } .swiper-container { width: 600px; height: 400px; border: 1px solid red; margin: 20px auto; } .swiper-slide { text-align: center; font-size: 18px; background: #fff; /* Center slide text vertically */ display: -webkit-box; display: -ms-flexbox; display: -webkit-flex; display: flex; -webkit-box-pack: center; -ms-flex-pack: center; -webkit-justify-content: center; justify-content: center; -webkit-box-align: center; -ms-flex-align: center; -webkit-align-items: center; align-items: center; } </ style > </ head > < body > < div class = "swiper-container1 swiper-container" > < div class = "swiper-wrapper" > < div class = "swiper-slide" >Slide 1</ div > < div class = "swiper-slide" >Slide 2</ div > < div class = "swiper-slide" >Slide 3</ div > </ div > <!-- Add Arrows --> < div class = "swiper-button-next" ></ div > < div class = "swiper-button-prev" ></ div > <!-- Add Pagination --> < div class = "swiper-pagination" ></ div > </ div > < div class = "swiper-container2 swiper-container" > < div class = "swiper-wrapper" > < div class = "swiper-slide" >Slide 1</ div > < div class = "swiper-slide" >Slide 2</ div > < div class = "swiper-slide" >Slide 3</ div > < div class = "swiper-slide" >Slide 4</ div > < div class = "swiper-slide" >Slide 5</ div > < div class = "swiper-slide" >Slide 6</ div > </ div > <!-- Add Arrows --> < div class = "swiper-button-next" ></ div > < div class = "swiper-button-prev" ></ div > <!-- Add Pagination --> < div class = "swiper-pagination" ></ div > </ div > < script > var swiper = new Swiper('.swiper-container', { slidesPerView: 1, spaceBetween: 30, loop: true, pagination: { el: '.swiper-pagination', clickable: true, }, navigation: { nextEl: '.swiper-button-next', prevEl: '.swiper-button-prev', }, }); </ script > </ body > </ html > |
2、两个轮播图效果不一致
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | <!DOCTYPE html> < html lang = "en" > < head > < meta charset = "UTF-8" > < meta name = "viewport" content = "width=device-width, initial-scale=1.0" > < title >Document</ title > < link href = "https://cdn.bootcss.com/Swiper/4.5.1/css/swiper.min.css" rel = "stylesheet" > < script src = "https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js" ></ script > < script src = "https://cdn.bootcss.com/Swiper/4.5.1/js/swiper.min.js" ></ script > < style > html, body { position: relative; height: 100%; } body { background: #eee; font-family: Helvetica Neue, Helvetica, Arial, sans-serif; font-size: 14px; color: #000; margin: 0; padding: 0; } .swiper-container1, .swiper-container2 { width: 600px; height: 400px; border: 1px solid red; margin: 20px auto; } .swiper-slide { text-align: center; font-size: 18px; background: #fff; /* Center slide text vertically */ display: -webkit-box; display: -ms-flexbox; display: -webkit-flex; display: flex; -webkit-box-pack: center; -ms-flex-pack: center; -webkit-justify-content: center; justify-content: center; -webkit-box-align: center; -ms-flex-align: center; -webkit-align-items: center; align-items: center; } </ style > </ head > < body > < div class = "swiper-container1 swiper-container" > < div class = "swiper-wrapper" > < div class = "swiper-slide" >Slide 1</ div > < div class = "swiper-slide" >Slide 2</ div > < div class = "swiper-slide" >Slide 3</ div > </ div > <!-- Add Arrows --> < div class = "swiper-button-next" ></ div > < div class = "swiper-button-prev" ></ div > <!-- Add Pagination --> < div class = "swiper-pagination" ></ div > </ div > < div class = "swiper-container2 swiper-container" > < div class = "swiper-wrapper" > < div class = "swiper-slide" >Slide 1</ div > < div class = "swiper-slide" >Slide 2</ div > < div class = "swiper-slide" >Slide 3</ div > < div class = "swiper-slide" >Slide 4</ div > < div class = "swiper-slide" >Slide 5</ div > < div class = "swiper-slide" >Slide 6</ div > </ div > <!-- Add Arrows --> < div class = "swiper-button-next" ></ div > < div class = "swiper-button-prev" ></ div > <!-- Add Pagination --> < div class = "swiper-pagination" ></ div > </ div > < script > var swiper = new Swiper('.swiper-container1', { slidesPerView: 1, spaceBetween: 30, loop: true, pagination: { el: '.swiper-pagination', clickable: true, }, navigation: { nextEl: '.swiper-button-next', prevEl: '.swiper-button-prev', }, }); var swiper = new Swiper('.swiper-container2', { slidesPerView: 2, spaceBetween: 30, slidesPerGroup: 2, loop: true, loopFillGroupWithBlank: true, pagination: { el: '.swiper-pagination', clickable: true, }, navigation: { nextEl: '.swiper-button-next', prevEl: '.swiper-button-prev', }, }); </ script > </ body > </ html > |
同学可以结合代码测试理解下。
如果我的回答帮助到了你,欢迎采纳,祝学习愉快~
3.WebAPP开发与小程序
- 参与学习 人
- 提交作业 622 份
- 解答问题 6815 个
微信带火了小程序,也让前端工程师有了更多的展现机会,本阶段带你从移动基础知识的学习到webAPP开发,及小程序开发,让你PC端与移动端两端通吃。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧