如何在 swiper.js 中更改滑动方向?
Posted
技术标签:
【中文标题】如何在 swiper.js 中更改滑动方向?【英文标题】:How to change swipe direction in swiper.js? 【发布时间】:2019-07-02 18:59:36 【问题描述】:当我将滑动器旋转 90 度时,我正在努力改变滑动方向。所以一开始默认它是水平方向的。单击幻灯片时,我正在旋转“swiper-container”使其全屏显示。所以它仍然有从左到右的滑动方向,但我需要从上到下改变而不将参数方向改变为垂直。
const topSwiper = new Swiper(this.DOMTopSwiper.current,
spaceBetween: 0,
observer: true,
observeParents: true,
observeSlideChildren: true,
direction: "vertical",
navigation:
nextEl: ".top-swiper-button-next",
prevEl: ".top-swiper-button-prev",
,
pagination:
el: ".zoom-amount-slides",
type: "fraction",
,
)
【问题讨论】:
你为什么不旋转容器,而是使用设备方向api并强制设备旋转?这是一个关于如何使用它的示例。jotform.com/blog/… 你为什么要轮换这个开始?我现在是否应该将头倾斜 90° 才能正确看到内容……? @anderson-ivan-witzke window.screen.orientation 是实验性 API,某些浏览器不支持when I rotate my swiper on 90deg
是什么意思?它是您从纵向旋转到横向的移动设备吗?
【参考方案1】:
销毁之前的 Swiper 并重新初始化 Swiper 并给出方向值。这里是doc
演示
let isVertical = true,
direction = 'vertical';
let swiper = initSwiper(direction);
function initSwiper(direction)
return new Swiper('.swiper-container',
spaceBetween: 50,
pagination:
el: '.swiper-pagination',
clickable: true,
,
direction: direction
);
function changeDirection()
isVertical = !isVertical;
direction = isVertical ? 'vertical' : 'horizontal';
let slideIndex = swiper.activeIndex;
swiper.destroy(true, true);
swiper = initSwiper(direction);
swiper.slideTo(slideIndex,0);
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: 400px;
height: 400px;
.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;
<!-- Link Swiper's CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.4.6/css/swiper.min.css">
<!-- Swiper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.4.6/js/swiper.js"></script>
<button onclick='changeDirection()'>Change Direction</button>
<!-- Swiper -->
<div class="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 Pagination -->
<div class="swiper-pagination"></div>
</div>
【讨论】:
@yerlan-yeszhanov 想要旋转图片本身,而不只是改变滑块方向 @Durga 感谢演示,但如上所述“没有将参数方向更改为垂直”。 @YerlanYeszhanov,你发现有什么问题吗? @Durga 你的意思是像一个立方体【参考方案2】:试试我的解决方案。
//Tested in Swiper 7.2.0
/**
* Insert to your page
*/
var rotate90Swiper = (function (swiper, optIsRotate90)
var isRoate90 = !!optIsRotate90
swiper.___touches =
swiper.___touches.currentX = swiper.touches.currentX
Object.defineProperty(swiper.touches, 'currentX',
set: function (v)
if (!isRoate90)
swiper.___touches.currentX = v
else
swiper.___touches.currentY = v
,
get: function ()
return swiper.___touches.currentX
)
swiper.___touches.currentY = swiper.touches.currentY
Object.defineProperty(swiper.touches, 'currentY',
set: function (v)
if (!isRoate90)
swiper.___touches.currentY = v
else
swiper.___touches.currentX = v
,
get: function ()
return swiper.___touches.currentY
)
return function (b)
isRoate90 = b
)
/**
* How to use
*/
var setRotate90 = rotate90Swiper(YourSwiperInstance, defaultIsRotate90)
setRotate90(false) // swiper rotate 90deg
setRotate90(true) //normal swiper
【讨论】:
以上是关于如何在 swiper.js 中更改滑动方向?的主要内容,如果未能解决你的问题,请参考以下文章
swiper.js我给swiper-slide里面的div设置高度后为啥