简体   繁体   English

尝试将过渡效果应用于悬停

[英]Trying to apply transition effect to hover

My CSS: 我的CSS:

a:hover {
    position: relative;
}
a:hover:after {
    z-index: -1;
    content: url(icon.jpg);
    display: block;
    position: absolute;
    left: 0px;
    bottom: 20px;
}

This displays an icon when I hover over an anchor, from this post: Make image appear on link hover css 当我将鼠标悬停在锚点上时,这将显示一个图标,来自此帖子: 使图像显示在链接悬停CSS上

I am trying to apply this: 我正在尝试应用此:

-webkit-transition: all 0.3s ease-in;
-moz-transition: all 0.3s ease-in;
-o-transition: all 0.3s ease-in;

So that the image fades in, but I cant get it to work. 这样图像就会淡入,但是我无法使其正常工作。

WebKit (Chrome, Safari) does not support transitions on pseudo elements. WebKit(Chrome,Safari)不支持在伪元素上进行过渡。 It should work in Firefox. 它应该可以在Firefox中使用。

see this q/a 看到这个q / a

To accomplish your need you could apply the background image for the link and in hover you could apply the transition by setting the background-position. 为了满足您的需要,您可以为链接应用背景图像,而悬停时可以通过设置背景位置来应用过渡效果。 You can also use an extra span inside the a tag instead of using :before pseudo class. 你也可以使用一个额外的spana标签,而不是使用:before伪类。

You could do a background image. 您可以制作背景图片。

a {
   -webkit-transition: all 0.3s ease-in;
   -moz-transition: all 0.3s ease-in;
   -o-transition: all 0.3s ease-in;
}
a:hover {
    position: relative;
    background:url(icon.jpg); 
}

The code is just an example, you would need to position the background image as well, since I dont know the dimensions of your design I can't tell you the exact position. 该代码只是一个示例,您还需要定位背景图片,因为我不知道您的设计尺寸,所以我无法告诉您确切的位置。

Webkit currently support transitions and animations Webkit当前支持过渡和动画

http://css-tricks.com/transitions-and-animations-on-css-generated-content/ http://css-tricks.com/transitions-and-animations-on-css-generated-content/

a:hover {
    position: relative;
}
a:after{
    -webkit-transition: all 0.3s ease-in;
    -moz-transition: all 0.3s ease-in;
    -o-transition: all 0.3s ease-in;
       transition: all 0.3s ease-in; /*never forget the standard*/
}
a:hover:after {
    z-index: -1;
    content: url(icon.jpg);
    display: block;
    position: absolute;
    left: 0px;
    bottom: 20px;
}

And the example used before: 以及之前使用的示例:

http://jsfiddle.net/d2KrC/88/ http://jsfiddle.net/d2KrC/88/

The example using image 使用图像的例子

http://jsfiddle.net/d2KrC/92/ http://jsfiddle.net/d2KrC/92/

There are some css "tricks" that can help you, maybe using css keyframes, but the best way to perform this in a compatibility way is using jQuery (a jquery version that matches your compat needs). 有一些css“技巧”可以帮助您,也许可以使用css关键帧,但是以兼容方式执行此操作的最佳方法是使用jQuery(符合您Compat需求的jquery版本)。 As some people asked you on css, where webkit actually support this kind of transitions, and this question could grow if we start talking on standards, the best you can do at first is update all your browsers and check. 正如有人在css上问您的那样,webkit实际上在哪里支持这种过渡,并且如果我们开始谈论标准,这个问题可能会越来越大,那么您最开始要做的就是更新所有浏览器并进行检查。

If you need or want to keep compat on older browser versions, you'll need to catch the hover event with javascript and then do whatever you want (as javascript can work directly with the DOM) and with CSS is pre-loaded and the most you can do is change the properties. 如果您需要或希望与旧版浏览器保持兼容,则需要使用javascript捕获悬停事件,然后执行所需的任何操作(因为javascript可以直接与DOM一起使用),并且CSS已预先加载,并且您可以做的就是更改属性。 ie load image with display: none, then change this property with an event. 即加载带有显示的图像:无,然后通过事件更改此属性。

example on jquery: jQuery上的示例:

 $('.link').click(function(){ $('.foo').fadeIn(); }); $('.link2').click(function(){ $('.foo2').fadeToggle(); if($('.link2').text() == 'show or hide') $('.link2').text('click again'); else $('.link2').text('show or hide'); }); 
 .foo, .foo2{display: none; width: 100px; height: auto;} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p> <img class="foo" src="http://joelbonetr.com/images/root.jpg" alt=""> <a class="link" href="#">show it!</a> </p> <p> <img class="foo2" src="http://joelbonetr.com/images/root.jpg" alt=""> <a class="link2" href="#">show or hide</a> </p> 

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

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