我在Swiper.js元素下面有一些内容。我尝试解除滑块上的touchmove事件的绑定,以便在移动设备上,用户可以在卡片的最后一张幻灯片上自由向下滚动页面。
然后,当它们位于页面顶部或附近时,或当swiper元素的顶部等于window.pageYOffset为0时,重新绑定触摸事件。我正在检测滚动方向,以便这个事件可能只需要在方向上升时发生。
Swiper提供了一个应该证明有用的回调onReadEnd
。
修改:API提供的内容还有onTouchMove
和onTouchMoveOpposite
要正确调试,请使用调试模式链接并在移动模拟器中使用开发工具,不要使用鼠标滚轮,只需单击拖动即可模拟触摸事件。
为了调试,我已经将swiper容器的高度设置为80% - 在生产中它将是100%的视口高度。
在解决方案中随意使用jQuery,它已经包含在项目中。
<div id="swiper" 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>
<div class="swiper-pagination"></div>
</div>
<div class="content">
<div class="container">
<h1>Header 1</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Molestias totam vero fuga dolorum explicabo suscipit aliquam hic blanditiis soluta officiis, corrupti consequatur nostrum sint possimus neque eos temporibus amet placeat.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Molestias totam vero fuga dolorum explicabo suscipit aliquam hic blanditiis soluta officiis, corrupti consequatur nostrum sint possimus neque eos temporibus amet placeat.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Molestias totam vero fuga dolorum explicabo suscipit aliquam hic blanditiis soluta officiis, corrupti consequatur nostrum sint possimus neque eos temporibus amet placeat.</p>
<h2>Header 2</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Molestias totam vero fuga dolorum explicabo suscipit aliquam hic blanditiis soluta officiis, corrupti consequatur nostrum sint possimus neque eos temporibus amet placeat.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Molestias totam vero fuga dolorum explicabo suscipit aliquam hic blanditiis soluta officiis, corrupti consequatur nostrum sint possimus neque eos temporibus amet placeat.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Molestias totam vero fuga dolorum explicabo suscipit aliquam hic blanditiis soluta officiis, corrupti consequatur nostrum sint possimus neque eos temporibus amet placeat.</p>
</div>
</div>
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: 100%;
height: 80%;
}
.swiper-slide {
text-align: center;
font-size: 22px;
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;
background-size: cover;
background-position: 50% 50%;
background-repeat: no-repeat;
}
.content {
padding: 30px 15px;
}
.container {
max-width: 1200px;
margin:0 auto;
}
var $el = {
swiper: document.getElementById('swiper')
}
var swiper = new Swiper('.swiper-container', {
pagination: '.swiper-pagination',
paginationClickable: true,
direction: 'vertical',
onReachEnd: function(swiperInstance) {
console.log('onReachEnd(), unbind touchmove')
},
onTouchMove: function(swiper, event) {
// Callback function, will be executed when user touch and move finger over Swiper. Receives swiper instance and 'touchmove' event as an arguments.
},
onTouchMoveOpposite: function(swiper, event) {
// Callback function, will be executed when user touch and move finger over Swiper in direction opposite to direction parameter. Receives swiper instance and 'touchmove' event as an arguments.
}
});
// Unsure if below will be needed, could be done all within swiper callbacks
var lastY = 0;
$el.swiper.addEventListener('touchmove', function(event) {
var currentY = event.touches[0].clientY;
if ( currentY > lastY ) {
console.log('up', currentY, lastY)
// top of page +/- 100px
// this is inaccurate, currentY is the Y pos of the touch, not
// equivalent to window.pageYOffset or scrollTop
if ( currentY <= 100 ) {
console.log('top of page, rebind touchmove')
}
} else if( currentY < lastY ) {
console.log('down')
}
lastY = currentY;
})
答案 0 :(得分:0)
我正在一个我遇到类似情况的网站上工作。您必须在初始化时设置属性mousewheelControl: true
,以便使用方法.disableMousewheelControl()
和.enableMousewheelControl()
。
链接到示例的暂存网站:http://multiply.website.2017.360southclients.net:8080/services/seo/
以下是相关的JS代码。 (fyi - freezePoint
计算考虑了粘性导航栏和幻灯片放映上方vh
大小的元素)
var serviceSwiper = new Swiper( '.swiper-container', {
direction: 'vertical',
slidesPerView: 1,
mousewheelControl: true,
mousewheelReleaseOnEdges: true,
mousewheelSensitivity: .05,
...
});
serviceSwiper.disableMousewheelControl();
var freezePoint = 0;
$( window ).on( 'resize', function( e ) {
freezePoint = Math.ceil( $( '#service-slides' ).offset().top - $( '#header' ).outerHeight() - 50 );
}).trigger( 'resize' );
$( window ).on( 'scroll', function( e ) {
if ( ! serviceSwiper.isEnd && window.scrollY >= freezePoint || ! serviceSwiper.isBeginning && window.scrollY <= freezePoint ) {
$( 'html' ).scrollTop( freezePoint );
if ( ! serviceSwiper.params.mousewheelControl ) {
serviceSwiper.enableMousewheelControl();
}
} else if ( serviceSwiper.params.mousewheelControl ) {
serviceSwiper.disableMousewheelControl();
}
});