简体   繁体   English

单击下划线动画

[英]Underline animation on click

I'm trying to recreate the animation on description and review here . 我正在尝试在描述中重新创建动画并在此处查看 The underline animates from right to left when the link is unclicked the underline animates from left to right before it disappears. 当链接未被点击时,下划线从右到左动画,下划线在它消失之前从左到右动画。

 a { color:#00f; text-decoration:none; display:inline-block; } a:after { width: 0; display:block; background:#00f; height:3px; transition: all .5s ease-in-out; content:""; } a:hover { color:#00f; } a:hover:after { width:100%; } 
 <a href="#">Click first</a> <a href="#">Click second</a> 

  1. Use position on the pseudoelement. 使用伪元素上的position Add position: relative to a , and position: absolute to :after . 添加position: relative对于a ,和position: absolute to :after

  2. Position the pseudoelement using bottom and right . 使用bottomright定位伪元素。 This means the line originates from the right side. 这意味着该行来自右侧。

  3. On hover/click, add the left property. 在悬停/单击时,添加left属性。 This makes the line span the full width. 这使得线条跨越整个宽度。 The width grows from 0 to 100% from left to right. 宽度从左到右从0100%增长。

When you 'unhover' or click again, left is removed, and the line originates from the right again, so the width decreases in that direction. 当您“取消悬停”或再次单击时, left被移除,并且该行再次从右侧开始,因此宽度在该方向上减小。

 $('a').on('click', function() { $('a').not(this).removeClass('underline'); $(this).toggleClass('underline'); }); 
 a { color: #00f; text-decoration: none; display: inline-block; position: relative; } a:after { width: 0; background: #00f; height: 3px; transition: all .5s ease-in-out; content: ""; position: absolute; bottom: -5px; right: 0; } a.underline:after { width: 100%; left: 0; } a:hover:after { width: 100%; left: 0; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="#">Click first</a> <a href="#">Click second</a> 

Maybe you want a HTML/CSS only solution: 也许你想要一个HTML / CSS唯一的解决方案:

 a { color: #00f; text-decoration: none; display: inline-block; } a span:after { width: 0; display: block; background: #00f; height: 3px; transition: all .5s ease-in-out; content: ""; float: right; } a span:focus { color: #00f; } label:hover { cursor: pointer; } label input { display: none; } label input:checked + span:after, label input + span:hover:after { width: 100%; float: left; } label input:checked + span { color: #00f; } 
 <a href="#"> <label> <input type="radio" name="link" /> <span> Click first </span> </label> </a> <a href="#"> <label> <input type="radio" name="link" /> <span> Click second </span> </label> </a> 

You can use the following. 您可以使用以下内容。 Just use a class to set style. 只需使用一个类来设置样式。

 $('a').click(function(e){ $(this).siblings().removeClass('start'); $(this).toggleClass('start'); e.preventDefault(); }) 
 a { color:#00f; text-decoration:none; display:inline-block; } a:after { width: 0; display:block; background:#00f; height:3px; transition: all .5s ease-in-out; content:""; float:right; } a:hover { color:#00f; } a.start:after { width: 100%; } a:hover:after { width: 100%; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="#">Click first</a> <a href="#">Click second</a> 

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

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