- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用边框动画(来自 CSSTricks 的 MagicLine),它工作正常但我面临的唯一挑战是,目前它开始第一个元素并为所有元素设置动画,但是如果我点击任何元素然后我从第一项添加相同的类并删除,但动画仅从第一项开始
这是 JS fiddle
请帮忙!!
注意:或者请建议任何纯 CSS 解决方案来实现这种动画
$(function() {
var $el, leftPos, newWidth;
$mainNav2 = $("#example-two");
$("#example-one").append("<li id='magic-line'></li>");
var $magicLine = $("#magic-line");
$magicLine
.width($(".current_page_item").width())
.css("left", $(".current_page_item a").position().left)
.data("origLeft", $magicLine.position().left)
.data("origWidth", $magicLine.width());
$("#example-one li").find("a").hover(function() {
$el = $(this);
leftPos = $el.position().left;
newWidth = $el.parent().width();
$magicLine.stop().animate({
left: leftPos,
width: newWidth
});
}, function() {
$magicLine.stop().animate({
left: $magicLine.data("origLeft"),
width: $magicLine.data("origWidth")
});
});
$("#example-one > li").click(function () {
alert(1);
var _this = $(this);
$("#example-one > li").removeClass('current_page_item');
_this.parent().addClass('current_page_item');
});
});
* { margin: 0; padding: 0; }
body { font: 14px Georgia, serif; background: #2F2626; color: #eee; }
header { padding: 100px 0 0 0; display: block; }
header h1 { width: 960px; margin: 0 auto; }
a { color: #eee; }
a:hover { color: white; }
.nav-wrap {
margin: 50px auto;
background-color: rgba(0,0,0,0.6);
border-top: 2px solid white;
border-bottom: 2px solid white;
}
/* Clearfix */
.group:after { visibility: hidden; display: block; content: ""; clear: both; height: 0; }
*:first-child+html .group { zoom: 1; } /* IE7 */
/* Example One */
#example-one {
margin: 0 auto;
list-style: none;
position: relative;
width: 960px;
}
#example-one li {
display: inline-block;
}
#example-one a {
color: #bbb;
font-size: 14px;
float: left;
padding: 6px 10px 4px 10px;
text-decoration: none;
text-transform: uppercase;
}
#example-one a:hover {
color: white;
}
#magic-line {
position: absolute;
bottom: -2px;
left: 0;
width: 100px;
height: 2px;
background: #ff0000;
}
.current_page_item a {
color: white !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<div class="nav-wrap">
<ul class="group" id="example-one">
<li class="current_page_item">
<a href="#">Home</a>
</li>
<li><a href="#">Buy Tickets</a></li>
<li><a href="#">Group Sales</a></li>
<li><a href="#">Reviews</a></li>
<li><a href="#">The Show</a></li>
<li><a href="#">Videos</a></li>
<li><a href="#">Photos</a></li>
<li><a href="#">Magic Shop</a></li>
</ul>
</div>
最佳答案
这是因为您没有在点击事件后设置魔术线元素的数据属性。
添加以下行:
$magicLine.data("origLeft", _this.position().left)
.data("origWidth", _this.width());
解决方案见下面的代码。
$(function() {
var $el, leftPos, newWidth;
$mainNav2 = $("#example-two");
/*
EXAMPLE ONE
*/
/* Add Magic Line markup via JavaScript, because it ain't gonna work without */
$("#example-one").append("<li id='magic-line'></li>");
/* Cache it */
var $magicLine = $("#magic-line");
$magicLine
.width($(".current_page_item").width())
.css("left", $(".current_page_item a").position().left)
.data("origLeft", $magicLine.position().left)
.data("origWidth", $magicLine.width());
$("#example-one li").find("a").hover(function() {
$el = $(this);
leftPos = $el.position().left;
newWidth = $el.parent().width();
$magicLine.stop().animate({
left: leftPos,
width: newWidth
});
}, function() {
$magicLine.stop().animate({
left: $magicLine.data("origLeft"),
width: $magicLine.data("origWidth")
});
});
$("#example-one > li").click(function() {
var _this = $(this);
$("#example-one > li").removeClass('current_page_item');
_this.parent().addClass('current_page_item');
// added lines below
$magicLine.data("origLeft", _this.position().left)
.data("origWidth", _this.width());
});
});
* {
margin: 0;
padding: 0;
}
body {
font: 14px Georgia, serif;
background: #2F2626;
color: #eee;
}
header {
padding: 100px 0 0 0;
display: block;
}
header h1 {
width: 960px;
margin: 0 auto;
}
a {
color: #eee;
}
a:hover {
color: white;
}
.nav-wrap {
margin: 50px auto;
background-color: rgba(0, 0, 0, 0.6);
border-top: 2px solid white;
border-bottom: 2px solid white;
}
/* Clearfix */
.group:after {
visibility: hidden;
display: block;
content: "";
clear: both;
height: 0;
}
*:first-child+html .group {
zoom: 1;
}
/* IE7 */
/* Example One */
#example-one {
margin: 0 auto;
list-style: none;
position: relative;
width: 960px;
}
#example-one li {
display: inline-block;
}
#example-one a {
color: #bbb;
font-size: 14px;
float: left;
padding: 6px 10px 4px 10px;
text-decoration: none;
text-transform: uppercase;
}
#example-one a:hover {
color: white;
}
#magic-line {
position: absolute;
bottom: -2px;
left: 0;
width: 100px;
height: 2px;
background: #ff0000;
}
.current_page_item a {
color: white !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="nav-wrap">
<ul class="group" id="example-one">
<li class="current_page_item">
<a href="#">Home</a>
</li>
<li><a href="#">Buy Tickets</a></li>
<li><a href="#">Group Sales</a></li>
<li><a href="#">Reviews</a></li>
<li><a href="#">The Show</a></li>
<li><a href="#">Videos</a></li>
<li><a href="#">Photos</a></li>
<li><a href="#">Magic Shop</a></li>
</ul>
</div>
关于jquery - Magicline 边框动画 - 从事件类开始动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54678924/
我有一个只有顶级元素的基本导航。有一个超大的背景图像(导航是 100 像素高,背景图像是 200 像素高)被添加到正在访问的当前页面。 Link 1 Link 2 Link 3 L
我在我的一个元素中使用了魔术线。除了一些下拉元素外,它工作得很好。当我单击某些下拉菜单时,它会重置到页面末尾。理想情况下,我希望它保持在父导航之上。 我正在使用 Bootstrap 下拉菜单。 HTM
我试图将带有底部边框的菜单更改为带有底部箭头。 例如:http://i.imgur.com/3onjcjV.png https://css-tricks.com/jquery-magicline-na
这是我曾经写过的代码。如果没有滚动,则它可以正常工作。但是当您单击滚动区域中的其余元素时,将无法正常工作。点击Link4后面的元素即可了解问题。 $(function() { $(".li").c
我使用 jQuery MagicLine 作为垂直滑动。我希望它是这样的,当我点击菜单时,该行将停留在该菜单项上,而不是像往常一样回到第一个菜单项。而且每当点击页面的向下箭头时,magicline 应
我只是试图通过删除我将在下面放置的 CSS 代码来删除内置在我的 Wordpress 主题中的 MagicLine。它起作用了,线路消失了,但现在我的导航全都跳动了,正如您在网站上看到的那样:http
我有 wordpress 网站,主页上有博客文章过滤功能。当您单击其中一个过滤器时 - 帖子会根据所选过滤器的类别发生变化。 我想做的是添加“MagicLine”,就像发布在这个 Fiddle 上的一
我正在尝试使用边框动画(来自 CSSTricks 的 MagicLine),它工作正常但我面临的唯一挑战是,目前它开始第一个元素并为所有元素设置动画,但是如果我点击任何元素然后我从第一项添加相同的类并
我尝试在我的网站上使用 MagicLine jQuery Navigation,但遇到了一个小问题。我希望下划线与当前突出显示的单词一样长。相反,它长了几个像素,我根本无法处理。我试图用 CSS 修复
我一直在尝试删除 CKEditor 中允许您插入段落的红线,但它顽固地拒绝删除自己。 在创建它的 javascript 中添加此代码: CKEDITOR.replace(target,{ re
我正在使用 Flexslider 进行幻灯片放映,我正在尝试使用 Magicline与它。 到目前为止一切正常,但是如果我单击箭头转到下一张幻灯片,magicline 不会移动 - 只有当我将鼠标悬停
有人熟悉 Magic Line jquery 菜单吗? http://css-tricks.com/jquery-magicline-navigation/ 我找不到任何关于如何让它在 Wordpre
第一次发帖。希望大家能帮忙。将不胜感激,我相信我会学到一些东西。此外,如果您在下面看到我的问题之外的任何其他冗余,请随时给我一个关于编码实践的新问题。我需要尽快学习尽可能多的东西。关于问题! 我有两个
我是一名优秀的程序员,十分优秀!