简体   繁体   English

使CSS悬停元素永久onclick

[英]Make CSS hover element permanent onclick

So I have created a little box with some CSS animation: 所以我创建了一个带有一些CSS动画的小盒子:

.boxtest 
{
width: 50px;
height: 50px;
background-color: green;
opacity: .2;
transition: opacity .8s, width .8s ease-out;
-moz-transition: opacity .8s, width .8s ease-out;
-webkit-transition: opacity .8s, width .8s ease-out;
-o-transition: opacity .8s, width.8s ease-out;
}

.boxtest:hover {
opacity: 1;
width: 70%;
}

What I'd like is for the CSS hover class to remain permanent after the user has hovered their mouse over the element. 我想要的是CSS悬停类在用户将鼠标悬停在元素上之后保持永久。

I guess you'd need to use Javascript, but I'm no expert so can't figure out the right command. 我想您需要使用Javascript,但是我不是专家,所以找不到正确的命令。 Any help would be awesome! 任何帮助都是极好的!

http://jsfiddle.net/r75gC/ http://jsfiddle.net/r75gC/

If you haven't a lot of experience with javascript I would recommend using JQuery. 如果您没有使用javascript的丰富经验,建议您使用JQuery。 Use this to include the JQuery libraries in your website: 使用它可以在您的网站中包含JQuery库:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

this will allow you to simply do (in your html file): 这将使您可以简单地执行操作(在html文件中):

<script> 
$(".boxtest").mouseenter(function() { $(".boxtest").addClass("boxtestHover"); });
</script>

also for the above change .boxtest:hover to boxtesthover (or whatever you want) 同样对于上述更改.boxtest:hover到boxtesthover(或任何您想要的)

Here you go! 干得好!

Basically I used jQuery to add a class to the div . 基本上,我使用jQuery向div添加一个类。 You can choose one of the two below. 您可以选择以下两个之一。

//onClick
$(".boxtest").on("click", function () {
  $(".boxtest").addClass('permahover');
});

//onHover
$(".boxtest").on("mouseenter", function () {
  $(".boxtest").addClass('permahover');
});

I changed the CSS to: 我将CSS更改为:

.boxtest:hover,
.permahover {
    opacity: 1;
    width: 70%;
}

http://jsfiddle.net/rFRc5/2/ http://jsfiddle.net/rFRc5/2/

jQuery is a bit overkill for this. jQuery对此有点矫over过正。

Instead of hover, use another class name, then just add this to the element 不用悬停,使用另一个类名,然后将其添加到元素中

onmouseover="this.className='newClassName'"

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

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