Reputation: 1105
I am trying to show an image when a thumbnail of it was clicked.
Here is my code as of now:
$('.theme-preview').click(function() {
console.log("fired");
var previewPopup = $(this).children('.popup-preview');
previewPopup.fadeIn("slow");
previewPopup
.css("position", "fixed")
.css("top", ($(window).height() / 2) - ($(this).children('.popup-preview').outerHeight() / 2))
.css("left", ($(window).width() / 2) - ($(this).children('.popup-preview').outerWidth() / 2));
return false;
});
$('html').click(function() {
$('.popup-preview').hide();
return false;
});
.popup-preview {
background: black;
position: absolute;
z-index: 1;
display: none;
}
.col-md-6 {
float: left;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
<div class="col-md-6 col-sm-6">
<div class="theme-preview"><img class="img-responsive center-block" src="http://lorempixel.com/output/technics-q-c-140-80-6.jpg">
<div class="popup-preview">
<div class="title">THEME 1-1</div>
<img src="http://lorempixel.com/output/technics-q-c-240-180-6.jpg" width="100%">
<div class="preview-btn">
<a href="#"> <span>Preview</span></a>
</div>
</div>
</div>
</div>
<div class="col-md-6 col-sm-6">
<div class="theme-preview"><img class="img-responsive center-block" src="http://lorempixel.com/output/technics-q-c-140-80-4.jpg">
<div class="popup-preview">
<div class="title">THEME 1-1</div>
<img src="http://lorempixel.com/output/technics-q-c-240-180-4.jpg" width="100%">
<div class="preview-btn">
<a href="#"> <span>Preview</span></a>
</div>
</div>
</div>
</div>
<div class="col-md-6 col-sm-6">
<div class="theme-preview"><img class="img-responsive center-block" src="http://lorempixel.com/output/technics-q-c-140-80-3.jpg">
<div class="popup-preview">
<div class="title">THEME 1-1</div>
<img src="http://lorempixel.com/output/technics-q-c-240-180-3.jpg" width="100%">
<div class="preview-btn">
<a href="#"> <span>Preview</span></a>
</div>
</div>
</div>
</div>
<div class="col-md-6 col-sm-6">
<div class="theme-preview"><img class="img-responsive center-block" src="http://lorempixel.com/output/technics-q-c-140-80-5.jpg">
<div class="popup-preview">
<div class="title">THEME 1-1</div>
<img src="http://lorempixel.com/output/technics-q-c-240-180-5.jpg" width="100%">
<div class="preview-btn">
<a href="#"> <span>Preview</span></a>
</div>
</div>
</div>
</div>
</div>
The images are showing when click on any of the thumbnails. And it will hide if click outside of it.
What I want to add is:
When I clicked a parent class (e.g. thumbnail #1) and shows the larger image, I want to hide the large image when I clicked again the parent class (e.g. thumbnail #1). As well as to other thumbnails.
Then when I clicked the thumbnail #1 and shows the larger image, and click the thumbnail #2, the large image of thumbnail #1 will hide and the larger image of thumbnail #2 will show. The same as well to other thumbnails.
Note: When a large image was clicked(itself), it should not be hidden.
Upvotes: 1
Views: 204
Reputation: 24915
You can try something like this:
$('.theme-preview').click(function() {
var previewPopup = $(this).children('.popup-preview');
if (previewPopup.is(":visible"))
previewPopup.fadeOut("slow");
else {
$('.popup-preview:visible').fadeOut();
previewPopup.fadeIn("slow");
previewPopup
.css({
"position": "fixed",
"top": ($(window).height() / 2) - (previewPopup.outerHeight() / 2),
"left": ($(window).width() / 2) - (previewPopup.outerWidth() / 2)
})
}
return false;
});
$('html').click(function() {
$('.popup-preview').hide();
return false;
});
$('.popup-preview').on("click", function(e){
e.stopPropagation()
})
.popup-preview {
background: black;
position: absolute;
z-index: 1;
display: none;
}
.col-md-6 {
float: left;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
<div class="col-md-6 col-sm-6">
<div class="theme-preview"><img class="img-responsive center-block" src="http://lorempixel.com/output/technics-q-c-140-80-6.jpg">
<div class="popup-preview">
<div class="title">THEME 1-1</div>
<img src="http://lorempixel.com/output/technics-q-c-240-180-6.jpg" width="100%">
<div class="preview-btn">
<a href="#"> <span>Preview</span></a>
</div>
</div>
</div>
</div>
<div class="col-md-6 col-sm-6">
<div class="theme-preview"><img class="img-responsive center-block" src="http://lorempixel.com/output/technics-q-c-140-80-4.jpg">
<div class="popup-preview">
<div class="title">THEME 1-1</div>
<img src="http://lorempixel.com/output/technics-q-c-240-180-4.jpg" width="100%">
<div class="preview-btn">
<a href="#"> <span>Preview</span></a>
</div>
</div>
</div>
</div>
<div class="col-md-6 col-sm-6">
<div class="theme-preview"><img class="img-responsive center-block" src="http://lorempixel.com/output/technics-q-c-140-80-3.jpg">
<div class="popup-preview">
<div class="title">THEME 1-1</div>
<img src="http://lorempixel.com/output/technics-q-c-240-180-3.jpg" width="100%">
<div class="preview-btn">
<a href="#"> <span>Preview</span></a>
</div>
</div>
</div>
</div>
<div class="col-md-6 col-sm-6">
<div class="theme-preview"><img class="img-responsive center-block" src="http://lorempixel.com/output/technics-q-c-140-80-5.jpg">
<div class="popup-preview">
<div class="title">THEME 1-1</div>
<img src="http://lorempixel.com/output/technics-q-c-240-180-5.jpg" width="100%">
<div class="preview-btn">
<a href="#"> <span>Preview</span></a>
</div>
</div>
</div>
</div>
</div>
Upvotes: 1
Reputation: 743
First you need to check if the popup is visible , if yes hide it if a user clicks on its thumbnail.If no , hide any popup that is open.And if a popup is visible and user clicks on other popup , still close any popup that is visible and show the corresponding popup.
$('.theme-preview').click(function() {
if($(this).children('.popup-preview').is(':visible')){
$(this).children('.popup-preview').hide();
}
else{
$('.popup-preview').hide();
var previewPopup = $(this).children('.popup-preview');
previewPopup.fadeIn();
previewPopup
.css("position", "fixed")
.css("top", ($(window).height() / 2) - ($(this).children('.popup-preview').outerHeight() / 2))
.css("left", ($(window).width() / 2) - ($(this).children('.popup-preview').outerWidth() / 2));
}
return false;
});
$('html').click(function() {
$('.popup-preview').hide();
return false;
});
.popup-preview {
background: black;
position: absolute;
z-index: 1;
display: none;
}
.col-md-6 {
float: left;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
<div class="col-md-6 col-sm-6">
<div class="theme-preview"><img class="img-responsive center-block" src="http://lorempixel.com/output/technics-q-c-140-80-6.jpg">
<div class="popup-preview">
<div class="title">THEME 1-1</div>
<img src="http://lorempixel.com/output/technics-q-c-240-180-6.jpg" width="100%">
<div class="preview-btn">
<a href="#"> <span>Preview</span></a>
</div>
</div>
</div>
</div>
<div class="col-md-6 col-sm-6">
<div class="theme-preview"><img class="img-responsive center-block" src="http://lorempixel.com/output/technics-q-c-140-80-4.jpg">
<div class="popup-preview">
<div class="title">THEME 1-1</div>
<img src="http://lorempixel.com/output/technics-q-c-240-180-4.jpg" width="100%">
<div class="preview-btn">
<a href="#"> <span>Preview</span></a>
</div>
</div>
</div>
</div>
<div class="col-md-6 col-sm-6">
<div class="theme-preview"><img class="img-responsive center-block" src="http://lorempixel.com/output/technics-q-c-140-80-3.jpg">
<div class="popup-preview">
<div class="title">THEME 1-1</div>
<img src="http://lorempixel.com/output/technics-q-c-240-180-3.jpg" width="100%">
<div class="preview-btn">
<a href="#"> <span>Preview</span></a>
</div>
</div>
</div>
</div>
<div class="col-md-6 col-sm-6">
<div class="theme-preview"><img class="img-responsive center-block" src="http://lorempixel.com/output/technics-q-c-140-80-5.jpg">
<div class="popup-preview">
<div class="title">THEME 1-1</div>
<img src="http://lorempixel.com/output/technics-q-c-240-180-5.jpg" width="100%">
<div class="preview-btn">
<a href="#"> <span>Preview</span></a>
</div>
</div>
</div>
</div>
</div>
Upvotes: 1