gpt4 book ai didi

javascript - Swiper.js - 在最后一张幻灯片上取消绑定(bind) touchmove 事件,当方向向上时重新绑定(bind) touchmove

转载 作者:行者123 更新时间:2023-11-28 05:40:59 28 4
gpt4 key购买 nike

我在 Swiper.js 元素下面有一些内容。我正在尝试取消绑定(bind) slider 上的 touchmove 事件,以便在移动设备上,用户在到达幻灯片的最后一张幻灯片时可以自由地向下滚动页面。

然后,当它们位于或接近页面顶部时,或者当 swiper 元素的顶部等于 window.pageYOffset 为 0 时,重新绑定(bind)触摸事件。我正在检测滚动方向,因此该事件可能只需要在方向向上时发生。

Swiper 提供了一个回调 onReadEnd,应该会很有用。

编辑:API 还提供了 onTouchMoveonTouchMoveOpposite

相关链接

要正确调试,请使用 Debug模式链接并使用移动模拟器中的开发工具,不要使用鼠标滚轮,仅通过单击拖动来模拟触摸事件。

出于调试目的,我已将 swiper 容器设置为 80% 高度 - 在生产中它将为 100% 视口(viewport)高度。

请随意在解决方案中使用 jQuery,它已包含在项目中。

HTML

<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>

CSS

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;
}

Javascript

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;
})

最佳答案

我正在一个网站上工作,我也遇到了类似的情况。您必须设置属性 mousewheelControl: true初始化时,您可以使用方法 .disableMousewheelControl().enableMousewheelControl() .

链接到示例的暂存站点:http://multiply.website.2017.360southclients.net:8080/services/seo/

下面是相关的JS代码。 (仅供引用 - 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();
}
});

关于javascript - Swiper.js - 在最后一张幻灯片上取消绑定(bind) touchmove 事件,当方向向上时重新绑定(bind) touchmove,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38924053/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com