简体   繁体   English

如何使用香草javascript或CSS设置边框动画

[英]How to animate border using vanilla javascript or css

I set border-bottom color to grey and add transiton to .3s, and ':hover' effect,so on hover it changes its color to blue. 我将边框底部的颜色设置为灰色,并在.3s中添加了transtrans,并添加了':hover'效果,因此在悬停时它将其颜色更改为蓝色。 Problem is related with that: when I place my mouse over the element with hover effect the border i set(blue) emerges from top to bottom(falling effect). 问题与此有关:当我将鼠标悬停在具有悬停效果的元素上时,我设置的边框(蓝色)从上到下出现(下降效果)。

this is it 就是这个

Now my client wants border to emerge from left to right on hover and border shouldn't fade away on hover on another list item,it(border) have to 'lag behind' the cursor trying to 'catch on' the cursor and fill the next list item's border positon. 现在我的客户希望边框在悬停时从左到右出现,并且边框不应在悬停在另一个列表项上时消失,它(边框)必须“落后于”光标以试图“抓住”光标并填充下一个列表项的边界位置。 I hope I you get what I want. 希望我能得到我想要的。 I know that it's not possible to do in css,but i don't know how to do this in js,can someone help me with that and expalain to me what to do,without using any frameworks like jQuery. 我知道这不可能在css中执行,但是我不知道如何在js中执行此操作,有人可以帮我解决这个问题并向我解释该怎么做,而不使用任何类似jQuery的框架。

You can animate by using the width property as Thusitha said. 您可以使用Suchitha所说的width属性进行动画处理。

Here I have attached the codepen link for that effect. 在这里,我附加了codepen链接以实现此效果。

 ul { list-style-type: none; border-bottom: 5px solid #eee; } ul li { display: inline-block; position: relative; padding: 20px; } ul li:before { content: ""; position: absolute; height: 5px; width: 0px; bottom: -5px; left: 0; background: blue; transition: all linear 0.25s; } /* Expanding Animation Element */ ul li:hover:before { width: 100% } 
 <div class="box"> <ul> <li>Homepage</li> <li>Style Demo</li> </ul> </div> 

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

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