This code allows you to resize the height
and width
of the image by running an increment
function when clicking the .resize
button. The problem is that I only want this functionality to work when in one of two states (list view vs. grid view).
How do I run the increment function in list
mode and remember the last size of the image when toggling back from grid
mode?
Currently if I toggle into grid mode I revert to the originalWidth
of the image but it resets the size of the list
mode image.
$(document).ready(function() {
var originalWidth = $("[data-view='list']")
.find(".listing__img")
.css("width");
function incSize(currentSize, incr, min, max) {
if (parseFloat(currentSize) === max) return originalWidth;
fSize = Math.max(Math.min(parseFloat(currentSize) + incr, max), min);
return fSize + "px";
}
$(".resize").click(function() {
var $list = $("[data-view='list']");
var xcontext = $(".resize");
var xactive = $(".resize__icon.active", xcontext);
var xnext = xactive.next();
xactive.removeClass("active");
xnext.addClass("active");
// Check if an element exists with both classes
if ($(".resize__icon.active").length == 0) {
// If not add active to the first .resize__icon
$(".resize__icon").first().addClass("active");
}
newFontSize = incSize(
$list.find(".listing__img").css("height"),
24,
46,
94
);
$list.find(".listing__img").css({
height: newFontSize,
width: newFontSize
});
});
$(".view__toggle--list").on("click", function() {
var $this = $(this).closest(".content");
$this.find(".listing__list").attr("data-view", "list");
$this.find(".view__toggle").removeClass("active");
$this.find(".view__toggle--list").addClass("active");
});
$(".view__toggle--grid").on("click", function() {
var $this = $(this).closest(".content");
$this.find(".listing__list").attr("data-view", "grid");
$this.find(".view__toggle").removeClass("active");
$this.find(".view__toggle--grid").addClass("active");
$(".listing__img").css({
width: originalWidth,
height: originalWidth
});
});
});
.resize {
cursor: pointer;
display: flex;
padding: 1.3rem;
border: 1px solid;
display: flex;
align-items: center;
}
.resize__icon {
background: #fafafa;
margin-right: 9px;
cursor: pointer;
border-radius: 3px;
}
.resize__icon--small {
height: 14px;
width: 14px;
}
.resize__icon--medium {
height: 18px;
width: 18px;
}
.resize__icon--large {
height: 24px;
width: 24px;
}
.resize__icon.active {
background: #4aabf0;
}
.listing__bottom {
border: 1px solid;
background: silver;
color: white
}
.listing__img {
height: 46px;
width: 46px;
border: 3px solid;
}
.listing__list {
padding: 1.3rem;
border: 1px solid;
}
.view {
display: flex;
padding: 0.9rem;
}
.view>div {
margin-right: 9px;
}
.view__toggle {
padding: .3rem;
cursor: pointer;
}
.view__toggle.active {
background-color: #ececec;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="content">
<div class="view">
<div id="view_list" class="view__toggle view__toggle--list active">
<div class="icon icon--list">list</div>
</div>
<div id="view_grid" class="view__toggle view__toggle--grid">
<div class="icon icon--grid">grid</div>
</div>
</div>
<div class="resize">
<div class="resize__icon resize__icon--small active"></div>
<div class="resize__icon resize__icon--medium"></div>
<div class="resize__icon resize__icon--large"></div>
</div>
<div class="listing__list" data-view="list">
<div class="listing__item">
<div class="listing__img">
</div>
</div>
<div class="listing__item">
<div class="listing__img">
</div>
</div>
<div class="listing__item">
<div class="listing__img">
</div>
</div>
</div>
</div>