I'm somewhat new to JavaScript / jQuery. I wrote this "plug-in", and it seems to do what I want. But I know what I've done is a bit clunky and inelegant.
The idea is that I have multiple columns of images, each column with a title. When you click on a column title, only that column of images shows. All the other columns disappear. And the selected column converts to a horizontal row. When the "all" button is clicked, the opposite happens. The selected horizontal row returns to being a column and all the others reappear.
I was hoping someone could show me a better way to write this.
The HTML is as follows:
<div class="row">
<div class="multi-show">
<div class="test-link">
<a href="#"><h4>Commercial</h4></a>
</div>
<div class="content-thumbs">
<div>
<div class="thumb">
<img class="w-100 img-thumbnail" src="pix/place-holder-1.gif" alt="">
</div>
<div class="thumb">
<img class="w-100 img-thumbnail" src="pix/place-holder-1.gif" alt="">
</div>
<div class="thumb">
<img class="w-100 img-thumbnail" src="pix/place-holder-1.gif" alt="">
</div>
<div class="thumb">
<img class="w-100 img-thumbnail" src="pix/place-holder-1.gif" alt="">
</div>
</div>
</div>
</div>
<div class="multi-show">
<div class="test-link">
<a href="#"><h4>Hospitality</h4></a>
</div>
<div class="content-thumbs">
<div class="thumb">
<img class="w-100 img-thumbnail" src="pix/place-holder-2.gif" alt="">
</div>
<div class="thumb">
<img class="w-100 img-thumbnail" src="pix/place-holder-2.gif" alt="">
</div>
<div class="thumb">
<img class="w-100 img-thumbnail" src="pix/place-holder-2.gif" alt="">
</div>
<div class="thumb">
<img class="w-100 img-thumbnail" src="pix/place-holder-2.gif" alt="">
</div>
</div>
</div>
</div>
The CSS is:
.multi-show {
position: relative;
width: 183px;
height: 530px;
float: left;
}
.single-show{
position: absolute;
width: 1200px;
height: 237px;
overflow: visible;
}
.test-link {
z-index: 10;
}
.thumb {
margin-bottom: 10px;
}
.thumb img {
border: none;
}
.img-thumbnail {
background-color: unset;
}
.content-thumbs {
position: relative;
height: 530px;
margin-top: 10px;
overflow-x: hidden;
overflow-y: scroll;
}
The original jquery was this:
$( document ).ready(function() {
$( ".test-link" ).on("click", function() {
var target = $(this).parent();
var pos = target.offset();
$(".thumbs-col").hide();
target.show();
target.css({
"background-color": "green",
"position": "absolute",
"left": pos,
"width": "1200px",
"height": "237px",
"overflow": "visible"
});
$(".content-thumbs .thumb").css({
"float": "left"
});
});
$( "#all" ).on("click", function() {
(".content-thumbs .thumb").css({
"float": "none"
});
console.log("good");
});
});
I've since changed it with some guidance (Thanks) to writing some external CSS styles and dynamically changing the classes. Like this:
$( document ).ready(function() {
var target;
$( ".test-link" ).on("click", function() {
target = $(this).parent();
$(".multi-show").hide();
target.show();
target.removeClass("multi-show");
target.addClass("single-show");
$(".content-thumbs .thumb").css({
"float": "left"
});
});
$( "#all" ).on("click", function() {
target.removeClass("single-show");
target.addClass("multi-show");
$(".content-thumbs .thumb").css({
"float": "none"
});
$(".multi-show").show();
console.log("good");
});
});
});
I'm guessing there is still a better way.