gpt4 book ai didi

javascript - 停止窗口滚动、滚动列表、在列表末尾恢复窗口滚动

转载 作者:行者123 更新时间:2023-12-02 21:24:25 25 4
gpt4 key购买 nike

我正在使用 jquery,我正在尝试在我的页面上实现一个滚动部分,您可以在其中循环列表。根据列表中哪个元素具有事件类,列表右侧的不同 div 会显示:inline-block,而前一个事件元素会显示:none。

HTML:

<div class="o-technologies">
<div class="o-technologies__list">
<div class="o-technologies__list--label">
<h2 class="">List</h2>
</div>
<div class="o-technologies__list--list">
<ul>

</ul>
</div>
</div>
<div class="o-technologies__content">
<div class="m-technology">
<div class="m-technology__wrapper">
<h2 class="m-technology__title">Technology 1</h2>
<div class="m-technology__description">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
</div>
</div>
<div class="m-technology">
<div class="m-technology__wrapper">
<h2 class="m-technology__title">Technology 2</h2>
<div class="m-technology__description">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
</div>
</div>
<div class="m-technology">
<div class="m-technology__wrapper">
<h2 class="m-technology__title">Technology 3</h2>
<div class="m-technology__description">
Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet
</div>
</div>
</div>
<div class="m-technology">
<div class="m-technology__wrapper">
<h2 class="m-technology__title">Technology 4</h2>
<div class="m-technology__description">
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.
</div>
</div>
</div>
</div>
</div>

jquery:

$(document).ready(function() {
$technologiesList = $(".o-technologies__list ul");

$('.m-technology').each(function(i, el) {
$technology = $(this).find('h2').text();
if ( i === 0) {
$(this).addClass('active');
}

$id = 'm-technology--' + $technology.toLowerCase();
$(this).attr('id', $id);

if ( i === 0) {
$technologyItem = "<li class='active'><a href='#" + $id + "'>" + $technology + "</a></li>";
} else {
$technologyItem = "<li><a href='#" + $id + "'>" + $technology + "</a></li>";
}

$technologiesList.append($technologyItem);
});
});

到目前为止,这有效,因此标题被收集并放入列表中。第一个列表项使类处于事件状态,从而赋予它不同的颜色。第一项技术也获得了事件类,从而使其显示:内联 block 。其余项目已显示:隐藏,因此它们不会显示。

现在,当我将鼠标悬停在 o-technologies div 上进行滚动时,我正在尝试使下一个列表项和 m-technology 获得事件类。

我可以通过使用鼠标悬停事件来停止窗口滚动并将正文溢出设置为隐藏,但我似乎无法使滚动事件正常工作,因为 o-technologies div 没有溢出。

代码笔:https://codepen.io/geordi-feijens/pen/RwPBxmK

最佳答案

您必须监听 ul 元素上的wheel事件,然后操作元素(添加和删除类)

下面是更新后的部分工作代码

$(document).ready(function() {
$technologiesList = $(".o-technologies__list ul");

$('.m-technology').each(function(i, el) {
$technology = $(this).find('h2').text();
if ( i === 0) {
$(this).addClass('active');
}

$id = 'm-technology--' + $technology.toLowerCase().replace(" ","");
$(this).attr('id', $id);

if ( i === 0) {
$technologyItem = "<li id='li-"+$id+"' class='active'><a href='#" + $id + "'>" + $technology + "</a></li>";
} else {
$technologyItem = "<li id='li-"+$id+"' ><a href='#" + $id + "'>" + $technology + "</a></li>";
}

$technologiesList.append($technologyItem);
});
$(".o-technologies__list--list ul").on("wheel",function(event){
if (event.originalEvent.wheelDelta > 0 || event.originalEvent.detail < 0) {
$currentActive = $(".o-technologies__list--list ul li.active");
$currentActive.prev().addClass('active')
$currentActive.removeClass('active')
$('#'+$currentActive[0].id.replace("li-","")).removeClass('active')
// console.log($currentActive.next()[0])
$('#'+$currentActive.prev()[0].id.replace("li-","")).addClass('active')
}
else {
$currentActive = $(".o-technologies__list--list ul li.active");
$currentActive.next().addClass('active')
$currentActive.removeClass('active')
$('#'+$currentActive[0].id.replace("li-","")).removeClass('active')
// console.log($currentActive.next()[0])
$('#'+$currentActive.next()[0].id.replace("li-","")).addClass('active')
}

})
});
.o-technologies {
display: flex;

&__list {
text-align: center;
flex: 0 0 50%;

&--label {

> * {
display: inline-block;
}
}
&--list {
ul {
list-style-type: none;
padding: 0;

li {
&.active {
a {
font-weight: 900;
color: blue;
}
}

a {
text-decoration: none;
color: black;
}
}
}
}
}

.m-technology {
width: 100%;
position: relative;
display: none;

&.active {
display: inline-block;
}

&__wrapper {
h2 {
margin-top: 0;
}
}
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<div class="o-technologies">
<div class="o-technologies__list">
<div class="o-technologies__list--label">
<h2 class="">List</h2>
</div>
<div class="o-technologies__list--list">
<ul>

</ul>
</div>
</div>
<div class="o-technologies__content">
<div class="m-technology">
<div class="m-technology__wrapper">
<h2 class="m-technology__title">Technology 1</h2>
<div class="m-technology__description">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
</div>
</div>
<div class="m-technology">
<div class="m-technology__wrapper">
<h2 class="m-technology__title">Technology 2</h2>
<div class="m-technology__description">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
</div>
</div>
<div class="m-technology">
<div class="m-technology__wrapper">
<h2 class="m-technology__title">Technology 3</h2>
<div class="m-technology__description">
Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet
</div>
</div>
</div>
<div class="m-technology">
<div class="m-technology__wrapper">
<h2 class="m-technology__title">Technology 4</h2>
<div class="m-technology__description">
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.
</div>
</div>
</div>
</div>
</div>

关于javascript - 停止窗口滚动、滚动列表、在列表末尾恢复窗口滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60771925/

25 4 0
文章推荐: javascript - 为什么注册按钮没有注册用户名和密码?
文章推荐: asp.net - 使用asp.net统计在线用户数
文章推荐: angular - 未知选项 : '--service-worker' creating new angular pwa
文章推荐: javascript - React -