简体   繁体   English

为什么CSS3:target无法正常工作?

[英]Why is css3 :target not working?

I have a div and I want that on click an other div slides in. I want to achieve this with css3 :target. 我有一个div,我希望单击另一个div使其滑入。我想使用css3:target实现此目的。

Like you can see in the code snippet the :hover works. 就像您在代码段中看到的那样::hover起作用了。 When hovered an animation start to fade some info in. When you click on the image I want that an other div fades in on the top by calling the same animations as used on hover. 悬停时,动画开始淡入一些信息。当您单击图像时,我希望另一个div通过调用与悬停时使用的相同动画在顶部淡入。

Can somebody help with getting the :target working? 有人可以帮助:target工作吗?

 body{ margin: 0px; } .image{ position: relative; width:300px; height:auto; } .image img{ width:100%; } .download{ position: absolute; top:0px; left:0px; height:80%; width:100%; background-color: gray; display:none; } .info{ position: absolute; bottom:0px; left:0px; height:20%; width:100%; background-color: green; opacity: 0; } .info p{ padding: 5px; margin: 0px; } .image:hover .info{ -webkit-animation: show 0.5s; /* Chrome, Safari, Opera */ animation: show 0.5s; opacity: 1; } .image:target .download{ -webkit-animation: show 0.5s; /* Chrome, Safari, Opera */ animation: show 0.5s; opacity: 1; } /* Chrome, Safari, Opera */ @-webkit-keyframes show { from {opacity: 0;} to {opacity: 1;} } /* Standard syntax */ @keyframes show { from {opacity: 0;} to {opacity: 1;} } 
 <div class="image"><!-- I detect everything on this div--> <img src="http://3.bp.blogspot.com/-gaBTIWFTWOo/UTeneLuwWyI/AAAAAAAABJE/E1GQBY4TJ8k/s1600/post-apocalypse-new-york.jpg"> <div class="download"><!-- this div should fade in on click (:target)--> download </div> <div class="info"> <!-- this div should fade in on hover--> <p>Image 1</p> </div> </div> 

This can be achieved via jquery: http://jsfiddle.net/swm53ran/13/ 这可以通过jquery实现: http : //jsfiddle.net/swm53ran/13/

$('.image').on('click', function(){
    $(this).find('.download').fadeIn(500);
});

i know you want to achieve this with :target, but you can do both animations through jquery to keep consistent if you would like. 我知道你想用:target来实现这一点,但是你可以通过jquery来做两个动画来保持一致。 just another option... 只是另一种选择...

i think i got it the way you wanted with css: 我想我以您想要的CSS方式实现了它:

added anchor tag surrounding image, added id #dl to download div...heres the fiddle http://jsfiddle.net/swm53ran/14/ 添加了周围图像的锚标签,添加了ID #dl以下载div ...此处是小提琴http://jsfiddle.net/swm53ran/14/

:target {
    display:block;
    -webkit-animation: show 0.5s; /* Chrome, Safari, Opera*/
    animation: show 0.5s;
    opacity: 1;
} 

<div class="image"><!-- I detect everything on this div-->
    <a href="#dl">
    <img src="http://3.bp.blogspot.com/-gaBTIWFTWOo/UTeneLuwWyI/AAAAAAAABJE/E1GQBY4TJ8k/s1600/post-apocalypse-new-york.jpg"/>
    <div class="download" id="dl"><!-- this div should fade in on click (:target)-->
        download
    </div>

    <div class="info"> <!-- this div should fade in on hover-->
        <p>Image 1</p>
    </div>
    </a>
</div>

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

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