gpt4 book ai didi

javascript - 锁定正文滚动时滚动 Div。溢出:Hidden?

转载 作者:太空宇宙 更新时间:2023-11-04 15:26:43 24 4
gpt4 key购买 nike

我正在使用ScrollStop创建一种 z 轴时间轴。它使用滚动事件来循环显示时间线内容。

代码在此处作为网站上传 http://jcwebandgraphic.com/archive2/

问题:

我使用了 mouseentermouseleave 来触发 CSS 更改 - 溢出隐藏和溢出可见,以防止在时间线 div 中滚动时在正文上滚动。

这已成功停止正文滚动,但它似乎也停止了允许我使用滚动事件在时间轴中移动的滚动停止功能。

这是因为滚动事件依赖于坐标输入,并且由于主体滚动被锁定,所以无法获取这些输入?难道是别的什么?您对这如何适用于我的元素有任何提示吗?这个元素有更好的事件监听器或滚动锁定方法吗?

代码:

    <script>
// When the Window Object is loaded and a Scroll Event is initiated, The following functions will Be Fired
$(window).on('scrollstart', function() {

// Timeline Functions for Touchless Devices

// Block 1 - Scroll Locking

// Locks Body Scrolling While in Timeline
$('#timeline').on('mouseenter', function() {
$('body').css('overflow', 'hidden');
}); // Closes $(#timeline) mouseenter Function

// Rerstores Body Scrolling While Out of Timeline
$('#timeline').on('mouseleave', function() {
$('body').css('overflow', 'visible');
}); // Closes $(#timeline) mouseleave Function
// Closes Functions Block 1

// Closes Block 1 - Scroll Locking

// Block 2 - Transitions

// Reassigns jQuery Defined HTML Objects as HTML Elements (For Removal and Re-Insertion to the DOM)
var last = $('.scroll li:last-child')[0]; // Convert to HTML Element jQuery Element
var parent = last.parentElement;

// Animate to 0 Opacity
$('.scroll li:last-child').animate({opacity: '.1'});

// Animation and Re-Ordering Block

// 1 Removes Last Child From the DOM
// 2 Forces Opacity Back to 1
// 3 Re-Inserts it into the DOM at the First Child Position

// 1 Removal
parent.removeChild(last);
// 2 Opacity
last.style.opacity = 1;
// 3 Re-insertion
parent.insertBefore(last, parent.firstElementChild)

// Closes Animation and Re-Ordering Block

// Closes Block 2 - Transitions

}); //Closes $(window) Load Functions

// Required Fixes

// 1 Size / Positioning Glitch on mouseenter, mouseleave (CSS, jQuery)
// 2 Restore Scroll Transitions on mouseenter (jQuery)
// 3 Touch Device Usability for Simulated or Alternate mouseenter and mouseleave Functions
// 4 Create Function Looping if Scroll Event lasts more than ~250ms
// 5 Make #timeline Height Responsive

// Completed Fixes

// 9 Scroll Only Works on First Refresh, or Directly from File
// 8 Functions Break When Removing Unnecessary HTML Content
// 7 Body Continues to Scroll While Scrolling Through Timeline
// 6 Collapsing Parent Element on #timeline
// 5 Last Child Failing to Re-Order
// 4 Limited Scroll Area Forces Premature Animation Transition Completion
// 3 Limited Scroll Area Prevents Opacity Completion
// 2 Uncaught Exception Errors (4)
// 1 Uncaught Token Errors (2)

</script>

<body>

<!-- Timeline -->
<section id='timeline'>
<h2 class='subtitle'>Timeline</h2>
<div class='scroll'>
<li><img src='images/pa3.jpg'></li>
<li><img src='images/pa2.jpg'></li>
<li><img src='images/pa1.jpg'></li>
</div>
</section>
<!-- Closes Timeline -->
</body>

最佳答案

尝试使用 body-scroll-lock ( https://www.npmjs.com/package/body-scroll-lock )。

在 body 上设置 overflow:hidden 在 iOS 上不起作用。您需要一些 JS 逻辑才能使其在那里工作。要锁定所有设备的正文滚动,同时使目标元素仍然能够滚动,您可以使用 body-scroll-lock 执行类似的操作:

// 1. Import the functions
const bodyScrollLock = require('body-scroll-lock');
const disableBodyScroll = bodyScrollLock.disableBodyScroll;
const enableBodyScroll = bodyScrollLock.enableBodyScroll;

// 2. Get a target element (such as a modal/lightbox/flyout/nav).
const targetElement = document.querySelector("#someElementId");


// 3. ...in some event handler after showing the target element...disable body scroll
disableBodyScroll(targetElement);


// 4. ...in some event handler after hiding the target element...
enableBodyScroll(targetElement);

有关基本原理的更多详细信息,请查看 https://medium.com/jsdownunder/locking-body-scroll-for-all-devices-22def9615177 .

希望有帮助。

关于javascript - 锁定正文滚动时滚动 Div。溢出:Hidden?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46837018/

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