- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在使用导航栏时遇到问题。在移动设备上,我希望在单击列表项后隐藏导航栏的内容。只有当您将窗口从大调整为小时才会出现此问题;当屏幕尺寸已经很小时,它不会发生。所以你必须点击“展开代码片段”才能看到这个问题。我认为我的 javaScript 代码有问题,但我不确定是什么。谢谢!
$(document).ready(function() {
var menuStatus = true;
var sections = $('section');
var nav = $('nav');
//smooth scroll
$("nav,#arrow").find('a').on('click', function() {
var el = $(this),
id = el.attr('href');
$('html, body').animate({
scrollTop: $(id).offset().top
}, 500);
});
//hide the mobile nav after a link is clicked
if ($(window).width() < 900) {
$("nav li a").on('click', function() {
$(".menu").hide();
$('nav input:checkbox:checked').prop('checked', false);
});
$("nav input").on('click', function() {
$(".menu").show();
});
}
//show desktop nav when the screen is at least 900px
$(window).on('resize', function() {
var win = $(this);
if (win.width() > 900) {
$(".menu").show();
}
});
});
nav {
position: absolute;
width: 100%;
z-index: 10;
}
nav img {
height: 150px;
}
nav ul {
list-style: none;
overflow: hidden;
}
.nav__container {
max-width: 1100px;
margin: 0 auto;
padding: 10px 40px;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: justify;
-ms-flex-pack: justify;
justify-content: space-between;
}
nav li a {
display: block;
padding: 20px 20px;
text-decoration: none;
color: #fff;
font-size: 110%;
text-transform: uppercase;
font-family: 'Rajdhani', sans-serif;
font-weight: 600;
}
nav ul li a:hover {
color: #5f3917;
background-color: #fbd802;
cursor: pointer;
}
a.active {
color: #1CAB1D;
}
nav .menu {
clear: both;
max-height: 0;
-webkit-transition: max-height .2s ease-out;
-o-transition: max-height .2s ease-out;
transition: max-height .2s ease-out;
}
nav .menu-icon {
margin-top: 5px;
cursor: pointer;
float: right;
padding: 28px 20px;
position: relative;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/*close icon*/
nav .menu-icon .navicon {
background: #b32025;
display: block;
height: 2px;
position: relative;
-webkit-transition: background .2s ease-out;
-o-transition: background .2s ease-out;
transition: background .2s ease-out;
width: 18px;
}
/*menu icon bottom and top lines*/
nav .menu-icon .navicon:before,
nav .menu-icon .navicon:after {
background: #b32025;
content: '';
display: block;
height: 100%;
position: absolute;
-webkit-transition: all .2s ease-out;
-o-transition: all .2s ease-out;
transition: all .2s ease-out;
width: 100%;
}
nav .menu-icon .navicon:before {
top: 5px;
}
nav .menu-icon .navicon:after {
top: -5px;
}
/* menu btn */
nav .menu-btn {
display: none;
}
/*height of vertical menu*/
nav .menu-btn:checked~.menu {
max-height: 300px;
}
nav .menu-btn:checked~.menu-icon .navicon {
background: transparent;
}
/*when clicked animate*/
nav .menu-btn:checked~.menu-icon .navicon:before {
-webkit-transform: rotate(-45deg);
-ms-transform: rotate(-45deg);
transform: rotate(-45deg);
}
/*when clicked animate*/
nav .menu-btn:checked~.menu-icon .navicon:after {
-webkit-transform: rotate(45deg);
-ms-transform: rotate(45deg);
transform: rotate(45deg);
}
nav .menu-btn:checked~.menu-icon:not(.steps) .navicon:before,
nav .menu-btn:checked~.menu-icon:not(.steps) .navicon:after {
top: 0;
}
/* MEDIA QUERIES */
@media only screen and (max-width: 730px) {
nav li:not(:last-child) {
border-right: none;
border-bottom: 1px solid rgba(252, 65, 65, 0.2);
}
nav ul {
padding: 0;
}
.nav__wrapper {
display: inline;
}
.container {
padding: 100px 20px;
}
}
@media only screen and (max-width: 730px) {
nav {
background-color: #fbd802;
}
nav img {
height: 70px;
}
nav li:not(:last-child) {
border-right: none;
border-bottom: 1px solid rgba(252, 65, 65, 0.2);
}
nav ul {
padding: 0;
}
nav li {
text-align: center;
}
.nav__container {
display: block;
}
}
@media only screen and (min-width: 730px) {
ul {
display: flex;
}
nav .menu {
max-height: none;
}
nav .menu-icon {
display: none;
}
}
.container {
max-width: 1100px;
margin: 0 auto;
padding: 100px 40px;
}
section {
height: 100vh;
}
#home {
background: red;
}
#aboutUs {
background: blue;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>
<section id="home">
<nav>
<div class="nav__container">
<img id="logo" src="https://via.placeholder.com/150" alt="logo">
<input class="menu-btn" type="checkbox" id="menu-btn" />
<label class="menu-icon" for="menu-btn"><span class="navicon"></span></label>
<ul class="menu">
<li><a href="#aboutUs">about us</a></li>
<li><a href="#menu">menu</a></li>
<li><a href="#gallery">gallery</a></li>
<li><a href="#contact">contact</a></li>
</ul>
</div>
</nav>
</section>
<section id="aboutUs">
</section>
</body>
</html>
最佳答案
我在运行您的脚本时发现以下错误:Uncaught TypeError: Cannot read property 'top' of undefined
。
有两种方法可以解决这个问题。添加 href 中列出的所有元素而不仅仅是 #aboutUs
,或者将您的 JS 更改为以下内容:
$("nav,#arrow").find('a').on('click', function() {
var elm_id = $(this).attr('href');
var section = $(elm_id);
if(section.length) {
$('html, body').animate({
scrollTop: section.offset().top
}, 500);
}
});
-- 编辑--
完整代码:
$(document).ready(function() {
// smooth scroll
$("nav, #arrow").on('click', 'a', function() {
var elm_id = $(this).attr('href');
var section = $(elm_id);
if(section.length) {
$('html, body').animate({
scrollTop: section.offset().top
}, 500);
}
});
// Handle Mobile Menu
var $window = $(window);
var $menu = $(".menu");
$("nav li a").on('click', function() {
if ($window.width() < 900) {
$menu.hide();
$('nav input:checkbox:checked').prop('checked', false);
}
});
$("nav input").on('click', function() {
if ($window.width() < 900) {
$menu.show();
}
});
// show desktop nav when the screen is at least 900px
$window.on('resize', function() {
if ($window.width() > 900) {
$menu.show();
} else {
$menu.hide();
}
});
});
关于javascript - 使用 jQuery 单击 anchor 标记时 div 内容不隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55657846/
当用户将鼠标悬停在 anchor 上时,除了悬停的 anchor 之外的每个 anchor 都会改变颜色。假设有 link1 link2 link3。如果用户将鼠标悬停在 link1 上,我希望更改
'; echo "".$value['eventsearchresultwhat'].""; echo "\n"; echo $value['eventsearchr
我有几个多列下拉菜单作为我的主导航的一部分。由于某种原因,两个多列下拉菜单中每个链接周围的 anchor 命中区域位于链接本身上方。我该如何修复它,使点击区域像普通 anchor 一样以链接文本为中心
我正在 android studio 中创建内部构建 AR 导航。我正在寻找一种方法将 anchor 与其他 anchor 或 anchor 节点/节点“连接”起来。我不确定使用哪一个。基于我将强制用
我有很多 anchor 可以在 doxygen 中描绘,例如 \anchor pic_foo \image html foo.gif "My Caption" \anchor pic_bar \ima
我有 3 个 anchor block ,当我加载页面时,我需要 3 个不同的随机 background-colors。然而,只有第一个 anchor 随机改变 background-color。这是
我有一个带有 float 顶部导航栏的网页。我想将 anchor 链接添加到目录以快速定向到页面上的其他位置。 #top { display: flex; z-index: 100; ba
我无法理解为什么将 text-align 属性设置为 right 值,将 span 标签移出 anchor 标签区域(溢出)? HTML文件的结构 a.btn{ border: 1px solid
我需要使用 Jquery 将类“Selected”添加到一个 anchor ,捕获它的唯一方法是通过其属性“active=true”。然后,当用户选择其他 anchor 时,新 anchor 将被设置
我在 hubspot 上有一个基于搜索词查询动态生成的搜索列表页面。 HTML结构如下: a class="hs-search-results__title" href="www.somethi
标题隐晦,但很难解释。使用 Firefox,前往 http://images.google.com进行随机搜索,然后将第一个搜索结果拖到地址栏。您会看到它转到具有图像 anchor 的页面。然后单击“
我正在为 vue-router 不滚动/导航到 anchor 标签(例如:#anchor)而苦苦挣扎。我在 Stack Overflow 上阅读了各种解决方案,但到目前为止都没有奏效。 请在下面找到我
我正在制作 AR 应用程序。要求很简单。在“配置阶段”,我启动应用程序并将一个立方体放在房间的地板上,然后关闭它。稍后当我再次启动应用程序时,当相机面向同一楼层时,立方体应该留在那里。根据我发现的信息
我正在 WordPress 中通过简码展示一组产品。显示屏上有图像和按钮。 问题:只有照片包含指向单个产品页面的链接。关联的按钮没有指向单个产品页面的链接。 这是当前代码:
我正在尝试做一件奇怪的事情。我将解释一般情况,然后按照我想要执行的步骤进行。我有一个 内的项目列表。当我按下一个元素时,必须打开一个模式。在这个模态中,有与 相同的元素。但详细解释了。当模式打开时
尝试从 anchor 克隆文本并将其设置为同一 anchor 的标题属性,但不了解如何使用正确语法的逻辑。 $(document).ready(function () { $('.desc a').c
--- #File A - Lunch: &01 - Chicken - Rice - Sallad ... --- #File B - Mon
我的用例是: 点击屏幕并将“点”保存为起始 anchor 第二次点击屏幕并将“点”保存为结束 anchor 按下按钮将对象从开始 anchor 移动到结束 anchor 我已经构建了自己的节点,该节点
我的菜单和基于#anchors 的导航有问题。我正在使用 JS 函数在滚动时将菜单置于顶部(#stickyheader)。我的菜单上有一个 div,滚动时会消失(#unstickyheader)。该
我目前有这个基础页面淡入淡出脚本函数,当前针对所有 anchor ('a'),但不确定如何准确排除具有类(.noFade)的某个 anchor 淡出。 JS: $(document).ready(fu
我是一名优秀的程序员,十分优秀!