简体   繁体   English

从左到右动画边框

[英]Animate border left to right

I am trying to animate a bottom border on a div so that it looks like a line sliding in from the right. 我正在尝试为div上的底部边框设置动画,以使其看起来像是一条从右侧滑入的线。

I am using jQuery but can't seem to work out how to achieve it, can anyone point me in the direction of a tutorial or reading on it? 我正在使用jQuery,但似乎无法解决如何实现它,有人可以指出我在教程或阅读它的方向吗?

This is not possible using a border. 使用边框是不可能的。 You can only animate the width of the bottom border (which appears to be it's height), not it's left/right position or horizontal width. 您只能设置底部边框的宽度(似乎是其高度)的动画,而不能设置其左/右位置或水平宽度的动画。

Instead, look at creating an absolutely positioned element within the element, and animating the width of that instead. 相反,要看在元素内创建一个绝对定位的元素,并为其设置动画宽度。

<div id="foo">
    Foo
    <div class="slider"></div>
</div>
#foo {
    font-size: 2em;
    position: relative;
    padding: 30px;
}
.slider {
    position: absolute;
    bottom: 0;
    left: 0;
    height: 2px;
    background-color: #C00;
    width: 0%;
}
$('.slider').animate({
    width: $('#foo').width()
}, 1000);

Example fiddle 小提琴的例子

Reference per Rory in fiddle 罗里(Rory)中的参考

HTML 的HTML

<div id="example"><div id="sample"></div></div>

JS JS

$( '#sample' ).animate({
width: "100%",
}, 1500 )

CSS 的CSS

#example{ height: 100px; width: 100px; }

#sample { border-bottom: 1px solid black; width: 0px; }

http://jsfiddle.net/p4zondkn/ http://jsfiddle.net/p4zondkn/

You don't need to use javascript. 您不需要使用javascript。 Using transitions, you can do the animation with css3, too. 使用过渡,您也可以使用css3进行动画处理。

fiddle http://jsfiddle.net/Drea/e7sf401q/ 小提琴 http://jsfiddle.net/Drea/e7sf401q/

relevant code: 相关代码:

html html

<div id="foo">hover me
    <div class="slider"></div>
</div>

css 的CSS

.slider{
    transition: width 2s ease;
    width: 0%;
 }

For a hover effect: 悬停效果:

#foo:hover .slider{
    width: 100%;
}

Or add a class with jquery, for use as on-click event. 或添加带有jquery的类,以用作单击事件。

.sliderAnimate {
    width: 100%;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM