简体   繁体   English

在点击时隐藏父div

[英]Hiding parent div on click

I'm enlarging images on click and moving them so they don't go off the page. 我要放大点击图像并移动它们,以使它们不会离开页面。 I have a parent div behind each that is black pic so that when I hover, I can change the opacity of the pic to make it look like it's darkening. 我在每个黑色图片的后面都有一个父div,以便在悬停时可以更改图片的不透明度,使其看起来像是变暗。 All of this works fine, however, when I enlarge and move the photo there is a black box left behind. 所有这些工作都很好,但是,当我放大并移动照片时,后面有一个黑框。 I need that to disappear but when I try it makes the child pic disappear too. 我需要使它消失,但是当我尝试它时,它也会使子照片也消失。

here's the code, jsfiddle posted beneath 这是代码,jsfiddle发布在下面

HTML 的HTML

<div id="Gpic1">
    <img class='galleryPics' id='pic1' src='http://i.imgur.com/urxD24P.jpg?1'>
</div>

CSS 的CSS

#Gpic1 {
float: left;
width: 187px;
height: 280px;
margin-left: 5%;
display: inline-block;
background: black;
padding: 0;
}

#pic1{
width: 187px;
height: 280px;
}

.enlarged {
    border: 10px solid #e5dbcc;
    position: absolute;
    -webkit-box-shadow: 7px 7px 5px rgba(50, 50, 50, 0.75);
    -moz-box-shadow: 7px 7px 5px rgba(50, 50, 50, 0.75);
    box-shadow: 7px 7px 5px rgba(50, 50, 50, 0.75);`
}

JQUERY JQUERY

 $('#Gpic1').hover(function () {
 if (!$(this).find('img').hasClass('enlarged')) {
     $(this).find('img').fadeTo(500, 0.5);
 }


}, function () {
     $(this).find('img').fadeTo(500, 1);
 });

 $('#pic1').click(function () {
     $(this).fadeTo(0, 1);
 if ($(this).hasClass('enlarged')) {
     $(this).removeClass('enlarged');

     $(this).stop().animate({
         width: 187,
         height: 280
     }, 0,

     function () {
         $(this).parent().removeClass('ontop');
     });
 } else {
     $(this).addClass('enlarged')
     $(this).parent().addClass('ontop');
     $(this).stop().animate({
         width: 533,
         height: 800,
         left: +590,
         bottom: +50
     }, 200);

 }

 });

When you try to remove #Gpic1 , all children are removed even if they are absolutely positioned. 当您尝试删除#Gpic1 ,即使绝对定位所有子项也将被删除。

If you just want the black background to go away, you can remove the black background from the containing div. 如果只想消除黑色背景,则可以从包含的div中删除黑色背景。

$("#Gpic1").css("background-color", "transparent");

Or, if you really want to remove the containing div, then you will need to make the image a child of some other object on the page. 或者,如果您确实要删除包含的div,则需要使图像成为页面上其他对象的子对象。 If you're using absolute positioning, then you could just make the image a child of the body object instead of #Gpic1 so then you can remove #Gpic1 and the image won't disappear. 如果使用绝对定位,则可以使图像成为主体对象的子级,而不是#Gpic1,这样就可以删除#Gpic1,并且图像不会消失。

// move pic1 to be a child of the body so we can remove Gpic1
$("#pic1").appendTo(document.body);
$("#Gpic1").remove();

Add this at the end of the animate for the pic onClick Event 在pic onClick事件的动画结尾添加此内容

$('#Gpic1').css('background','none');

Here is a fiddle. 这是一个小提琴。

http://jsfiddle.net/Td6tT/3/ http://jsfiddle.net/Td6tT/3/

$('#Gpic1').css('background','none');

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

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