简体   繁体   English

使用外部JavaScript文件触发HTML类

[英]Trigger a HTML-class with external javascript file



I'm using these icons on my webpage: http://fontawesome.io/examples/ 我在网页上使用这些图标: http : //fontawesome.io/examples/
One preset class they use for their icons is fa-spin , and it will make the icon spin (du-uh). 他们用于图标的一种预设类是fa-spin ,它将使图标旋转(du-uh)。
I want to make the icons spin whenever they're hovered. 我想让图标悬停时旋转。 A snippet of my HTML(The class "fa fa-envelope" triggers the icon itself): 我的HTML的摘要(类"fa fa-envelope"触发图标本身):

<a href="http://google.com" class="navbar-item">                
    <span class="icon">
        <i id="wantspin" class="fa fa-envelope fa-spin" onmouseover="dothis()"></i>
    </span>
</a>


Gotta make the fa-spin -class trigger somehow by the function onmouseover , right? 可以通过onmouseover函数以某种方式使fa-spin类触发,对吗?

Here's my javascript code, I know that I haven't done anything yet, other than linking the id of the <i> with the variable x , but I'm kinda lost. 这是我的javascript代码,我知道除了将<i>id与变量x链接之外,我还没有做任何其他事情,但是我有点迷失了。

function dothis() 
{
var x = document.getElementById ("wantspin")
} 


I hope it makes sense, and you get the question. 我希望这是有道理的,然后您就可以了。 I appreciate any help, insight, tips and tricks, thanks. 感谢您的帮助,见解,技巧和窍门,谢谢。

You can grab the fa-spin rule from font awesome's stylesheet and add a :hover selector: 您可以从awesome字体的样式表中获取fa-spin规则,然后添加:hover选择器:

 .spin:hover { -webkit-animation: fa-spin 2s infinite linear; animation: fa-spin 2s infinite linear } 
 <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/> <a href="http://google.com" class="navbar-item"> <span class="icon"> <i class="fa fa-envelope spin"></i> </span> </a> 

Chris G's answer is better. 克里斯·格的答案更好。 Here is another answer along the same grain of what you were trying to do. 这是您尝试做的事情的另一答案。 An even better approach would be to look into jquery and use unobtrusive event handlers instead. 更好的方法是调查jquery并改用不引人注目的事件处理程序。

You can ignore my HTML, it's just there so the javascript example works. 您可以忽略我的HTML,它就在那里,因此javascript示例可以正常工作。

 function dothis() { var el = document.getElementById('wantspin'); if (el) { el.classList.add('fa-spin'); } } function dothat() { var el = document.getElementById('wantspin'); if (el) { el.classList.remove('fa-spin'); } } 
 #wantspin { height: 20px; width: 20px; background: blue; } .fa-spin { border: 3px solid red; } 
 <a href="http://google.com" class="navbar-item"> <span class="icon"> <div id="wantspin" class="fa fa-envelope" onmouseover="dothis()" onmouseout="dothat()"></div> </span> </a> 

To do this just add and remove the class in the javaScript and add a onmouseout function to your element. 为此,只需在javaScript中添加和删除类,然后在元素中添加onmouseout函数即可。 Your html will be this (Notice no fa-spin class). 您的html就是这个(注意,没有fa-spin类)。

<a href="http://google.com" class="navbar-item">
    <span class="icon">
        <i id="wantspin" class="fa fa-envelope" onmouseover="dothis()" onmouseout="stopthis(this)"></i>
    </span>
</a>

And your javascript. 还有你的javascript。

function dothis() {
    var x = document.getElementById("wantspin");
    x.classList.add("fa-spin");
} 
function stopthis() {
    var x = document.getElementById("wantspin");
    x.classList.remove("fa-spin");
} 

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

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