简体   繁体   English

通过切换使图像显示在另一个div onclick上

[英]Getting an image to appear over another div onclick with a toggle

I am trying to be able to click on a specific square and get a checkmark to appear over-top of it. 我试图能够单击一个特定的正方形,并获得一个选中标记以显示在其上方。 I am going to want it to have a toggle effect, but I wanted to try to at least get the checkmark to show, which I am really having an issue getting it to even appear. 我希望它具有切换效果,但是我想尝试至少使选中标记显示出来,而实际上使它出现甚至是一个问题。

What am I doing wrong? 我究竟做错了什么?

 $('.package-img').click(function () { //target.innerHTML = '<img src="images/checkmark-circle.png" class="checkmark-img total-center">'; $('.package-img').prepend('<img src="https://d30y9cdsu7xlg0.cloudfront.net/png/835-200.png" class="checkmark-img">') }); 
 .package-img { width: 60%; height: auto; opacity: 1; margin-left: 20%; cursor: pointer; transition:1s; -webkit-transition:1s; position: relative; } #calendar-wrap, #tp-wrap { width: 100%; position: relative; } .checkmark-img { width: 50%; height: 50%; z-index: 1; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="calendar-wrap"> <h2 class="product-titles">Package 1</h2> <img src="http://jwilson.coe.uga.edu/emt725/SqToAcuteTri/Square.gif" alt="Package 1" class="package-img" id="calendar-img"> </div> <div id="calendar-wrap"> <h2 class="product-titles">Package 2</h2> <img src="http://jwilson.coe.uga.edu/emt725/SqToAcuteTri/Square.gif" alt="Package 2" class="package-img" id="tp-img"> </div> 

Use before method instead of prepend , otherwise your image is being added inside another image. 使用before方法代替prepend ,否则您的图像将被添加到另一个图像中。 Also use $(this).before... otherwise your checkmark will be added to all images with class package-img 还要使用$(this).before...否则,您的选中标记将被添加到所有带有package-imgpackage-img

$(this).before('<img src="https://d30y9cdsu7xlg0.cloudfront.net/png/835-200.png" class="checkmark-img">')

 var $img = $('<img src="https://d30y9cdsu7xlg0.cloudfront.net/png/835-200.png" class="checkmark-img"/>').hide(); $('.package-img').before($img); $('.package-img').click(function () { $(this).prev().show(); }); $('.checkmark-img').click(function () { $(this).hide(); }); 
 .package-img { width: 60%; height: auto; opacity: 1; margin-left: 20%; cursor: pointer; transition:1s; -webkit-transition:1s; position: relative; } #calendar-wrap, #tp-wrap { width: 100%; position: relative; } .checkmark-img { width: 50%; height: 100%; z-index: 1; position: absolute; left: 25%; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="calendar-wrap"> <h2 class="product-titles">Package 1</h2> <img src="http://jwilson.coe.uga.edu/emt725/SqToAcuteTri/Square.gif" alt="Package 1" class="package-img" id="calendar-img"> </div> <div id="calendar-wrap"> <h2 class="product-titles">Package 2</h2> <img src="http://jwilson.coe.uga.edu/emt725/SqToAcuteTri/Square.gif" alt="Package 2" class="package-img" id="tp-img"> </div> 

It is understood what you are looking for. 了解您在寻找什么。 Basically you can "toggle" the checkmark image over the checkmark square image when you click on the checkmark square. 基本上,当您单击选中标记正方形时,可以在选中标记正方形图像上方“切换”选中标记图像。 The problem with this is once you put the checkmark over the square the toggle doesnt work anymore. 问题是一旦将选中标记放在正方形上,切换开关将不再起作用。 I have come up with a solution that should work a little better. 我想出了一个应该会更好一些的解决方案。

Basically if we take advantage of the rules that when a label is selected that is bound to a <input type="checkbox" /> this will toggle the checked property of the checkbox. 基本上,如果我们利用以下规则,当选择绑定到<input type="checkbox" />label时,这将toggle复选框的checked属性。

We can then bind to this event in jQuery and show\\hide the checkmark. 然后,我们可以在jQuery中绑定到此事件并显示\\隐藏选中标记。 In this instance we dont bind any click events to the images, but the change event of the checkbox. 在这种情况下,我们不将任何点击事件绑定到图像,而是将复选框的change事件绑定。 Something like the example below. 类似于下面的示例。

Now the question is: 现在的问题是:

Why did i use a checkbox 我为什么要使用复选框

Well by using a checkbox we can capture the result in our form posts. 通过使用复选框,我们可以将结果捕获到表单中。 It also makes it easy to identify what is checked by enumerating all the check boxes that have the checked property. 通过枚举具有被checked属性的所有复选框,还可以轻松识别被选中的内容。

To have this sent to your server you will need to add a name property to each checkbox and also set the value property. 要将其发送到您的服务器,您需要在每个复选框中添加一个name属性,并设置value属性。

 $('.calendar-check').on('change', function() { if ($(this).prop('checked')) $(this).parents('.calendar-wrap:first').find('.checkmark-img').show(); else $(this).parents('.calendar-wrap:first').find('.checkmark-img').hide(); }); 
 .package-img { width: 60%; height: auto; opacity: 1; margin-left: 20%; cursor: pointer; transition: 1s; -webkit-transition: 1s; position: relative; } #calendar-wrap, #tp-wrap { width: 100%; position: relative; } .checkmark-img { display: none; z-index: 1; position: absolute; top: 50%; left: 50%; margin-top: -100px; margin-left: -100px; } .calendar-check { display: none; } .package-check-toggle { position: relative; height: 100%; width: 100%; display: block; cursor: pointer; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="calendar-wrap" class="calendar-wrap"> <h2 class="product-titles">Package 1</h2> <label for="package-check-1" class="package-check-toggle"> <img src="http://jwilson.coe.uga.edu/emt725/SqToAcuteTri/Square.gif" alt="Package 1" class="package-img" id="calendar-img" /> <img src="https://d30y9cdsu7xlg0.cloudfront.net/png/835-200.png" alt="Package 1" class="checkmark-img" /> </label> <input type="checkbox" class="calendar-check" id="package-check-1" /> </div> <div id="calendar-wrap" class="calendar-wrap"> <h2 class="product-titles">Package 2</h2> <label for="package-check-2" class="package-check-toggle"> <img src="http://jwilson.coe.uga.edu/emt725/SqToAcuteTri/Square.gif" alt="Package 2" class="package-img" id="calendar-img" /> <img src="https://d30y9cdsu7xlg0.cloudfront.net/png/835-200.png" alt="Package 2" class="checkmark-img" /> </label> <input type="checkbox" class="calendar-check" id="package-check-2" /> </div> 

Sample JSFiddle 样本JSFiddle

EDIT For completness sakes I figured best to show a CSS (3) only solution. 编辑为了完整起见,我想出了一个最好的CSS(3)解决方案。 This has no depenedcies on using jQuery at all. 这与使用jQuery完全无关。 Again this takes into consideration a checkbox (which is value based) and toggles the checkmark based on the toggle. 再次考虑到一个复选框(基于值),并基于切换来切换该复选标记。

The Markup is quite simple. 标记非常简单。

<div class="calendar-checkmark">
    <input type="checkbox" id="checkbox-1" name="checkbox1" value="true" />
    <label for="checkbox-1"></label>
</div>

Example: 例:

 .calendar-checkmark input[type=checkbox] { display: none; } .calendar-checkmark label { cursor:pointer; display: block; height: 384px; width: 354px; position:relative; background: url('http://jwilson.coe.uga.edu/emt725/SqToAcuteTri/Square.gif') no-repeat center center transparent; } .calendar-checkmark label::after { content:' '; display: block; height: 100%; width: 100%; position: absolute; z-index:1; display:none; background: url('https://d30y9cdsu7xlg0.cloudfront.net/png/835-200.png') no-repeat center center transparent; } .calendar-checkmark input[type=checkbox]:checked + label::after { display: block; } 
 <div class="calendar-checkmark"> <input type="checkbox" id="checkbox-1" name="checkbox1" value="true" /> <label for="checkbox-1"></label> </div> 

The trick here is using Adjacent sibling selectors which allow you to select an adjacent sibling based of another element. 这里的技巧是使用相邻的同级选择器 ,这些选择器允许您基于另一个元素选择相邻的同级。 So if we look at the css when the checkbox is checked it gets the Psudo-class :checked applied to it (similar to :hover etc). 因此,如果我们在选中复选框时查看css,它将得到伪类 :checked应用于它(类似于:hover等)。 Then we use the adjacent + selector to locate the label . 然后,使用相邻的+选择器定位label

Now the label element has a css background set to the image of the square box. 现在,label元素的css背景设置为方形框的图像。 Next it has the psudo-class ::after applied which creates our psudo-element that contains the checkbox. 接下来,它应用了psudo-class ::after ,它创建了包含复选框的psudo-element。 This is added after the element, when the checkbox is checked the psudo class :checked is addeed. 这是在元件中,当该复选框被之后添加checked的psudo类:checked被addeed。 This then changes the state of the psudo element by displaying the element. 然后,通过显示该元素来更改psudo元素的状态。

Another Fiddle 另一个小提琴

This can be a lot simpler if we just remove the box images and create them via CSS. 如果我们仅删除框形图像并通过CSS创建它们,则可以简单得多。 There will be less to download and we can then make the box's background transparent. 下载的内容将更少,然后我们可以使盒子的背景透明。 This will allow us to just show and hide the checkmark that is already sitting behind each box. 这将使我们能够仅显示和隐藏已位于每个框后面的对勾标记。

 $('.package-img').click(function () { this.nextElementSibling.classList.toggle("hide"); }); 
 parent { position:relative; } .package-img { border:3px solid black; width: 110px; height:110px; background-color:rgba(0,0,0,0); margin-left: 20%; cursor: pointer; position:relative; display:inline-block; } .checkmark-img { transition:1s; width: 15%; z-index: -1; position:relative; margin-left:-15%; } img.hide { display:none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <h2 class="product-titles">Package 1</h2> <div class="parent"> <div class="package-img"></div> <img src="https://d30y9cdsu7xlg0.cloudfront.net/png/835-200.png" class="checkmark-img hide"> </div> <h2 class="product-titles">Package 2</h2> <div class="parent"> <div class="package-img"></div> <img src="https://d30y9cdsu7xlg0.cloudfront.net/png/835-200.png" class="checkmark-img hide"> </div> 

1. absolutely position the Checkmark on top of the package image 1.绝对将对勾标记放在包装图片的顶部

2. Hide it 2.隐藏它

3. Use toggle to reveal it 3.使用切换显示它

since its hidden on top of it you will need to use the siblings selector to retrieve it 因为它隐藏在它的顶部,所以您将需要使用同级选择器来检索它

  $('.checkmark-img').hide(); $('.package-img').click(function () { $('.checkmark-img').toggle(); }); 
 .package-img { width: 60%; height: auto; opacity: 1; margin-left: 20%; cursor: pointer; transition:1s; -webkit-transition:1s; position: relative; } #calendar-wrap, #tp-wrap { width: 100%; position: relative; } .checkmark-img { width: 60%; height: auto; opacity: 1; margin-left: 25%; margin-top:10%; cursor: pointer; transition:1s; -webkit-transition:1s; position: absolute; width: 50%; height: 50%; z-index: 1; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="calendar-wrap"> <h2 class="product-titles">Package 1</h2> <img src="https://d30y9cdsu7xlg0.cloudfront.net/png/835-200.png" class="checkmark-img">' <img src="http://jwilson.coe.uga.edu/emt725/SqToAcuteTri/Square.gif" alt="Package 1" class="package-img" id="calendar-img"> </div> <div id="calendar-wrap"> <h2 class="product-titles">Package 2</h2> <img src="https://d30y9cdsu7xlg0.cloudfront.net/png/835-200.png" class="checkmark-img">' <img src="http://jwilson.coe.uga.edu/emt725/SqToAcuteTri/Square.gif" alt="Package 2" class="package-img" id="tp-img"> </div> 

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

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