javascript - 元素像亚马逊帮助页面一样保持悬停
问题描述
我想做类似亚马逊帮助页面的东西:
https://www.amazon.fr/gp/help/customer/display.html/ref=nav_cs_help?ie=UTF8&nodeId=508510#nav-top
当鼠标在某一类时,我们可以在右侧看到关注内容
我的代码几乎可以工作,但是,当我的鼠标离开链接时,右 div 不再处于活动状态
有我的代码:
网页:
<div class="activity--js margin-card activity-card card card-body">
<div class="row">
<div class="col-3 help__titles-part">
<% @helps.each do |help|%>
<p class="help__title" data-id='<%=help.id %>'><%=help.title%></p>
<%end%>
</div>
<div class="col-9 help__contents-part" >
<div class="row">
<% @helps.each do |help|%>
<div class="col-12 help__content-<%=help.id %>" style="display:none">
<p><%=help.content%></p>
</div>
<%end%>
</div>
</div>
</div>
</div>
JS:
$('.help__title').hover(
function () {
$(this).toggleClass('help__title-active');
Id = $(this).data("id");
console.log(Id);
$('.help__content-' + Id).toggleClass('help__content-active');
});
解决方案
您要求它像亚马逊示例一样,它使用 mouseenter 事件,而不是单击,也绝对不是悬停。
jQuery(document).ready(function($){
$('.help__title').mouseenter(function(){
$('.help__title-active').removeClass('help__title-active');
$('.help__content-active').removeClass('help__content-active');
$(this).addClass('help__title-active');
Id = $(this).attr("data-id");
$('.help__content-' + Id).addClass('help__content-active');
});
});
.help__title-active{font-weight:bold; color:red}
.help__content-active{display:block !important;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="activity--js margin-card activity-card card card-body">
<div class="row">
<div class="col-3 help__titles-part">
<p class="help__title" data-id='1'>Title 1</p>
<p class="help__title" data-id='2'>Title 2</p>
<p class="help__title" data-id='3'>Title 3</p>
<p class="help__title" data-id='4'>Title 4</p>
</div>
======================================================================
<div class="col-9 help__contents-part" >
<div class="row">
<div class="col-12 help__content-1" style="display:none">
<p>Help 1</p>
</div>
<div class="col-12 help__content-2" style="display:none">
<p>Help 2</p>
</div>
<div class="col-12 help__content-3" style="display:none">
<p>Help 3</p>
</div>
<div class="col-12 help__content-4" style="display:none">
<p>Help 4</p>
</div>
</div>
</div>
</div>
推荐阅读
- list - 如何根据自定义 Date Dart 对列表进行排序
- java - 使用 MapStruct 通过定位值进行枚举查找
- bash - 如何从文本文件中读取环境变量?
- python - 使用 Flask 请求上下文进行用户管理
- gradle - Jacoco nodeps:ClassNotFound com.vladium.emma.rt.RT
- python - Python Selenium Web-Driver 找不到文本元素
- visual-studio-code - VS Code 无法在网络驱动器上打开项目进行定义
- python - 如何将此字符串列表转换为整数
- django - 在Django中查看celery的定时任务
- graphql - 如何在 FaunaDB 中使用 REST API?