I want to change the height of a div with a button using jQuery only on small window. On larger screens, I want the div height to always remain the original. The button is visible on small screen.
Here is what I have tried, but need to refresh the page again and again when the window is resized.
Is there also any solution using JavaScript only.
function resize() {
if (window.innerWidth <= 700) {
$('.btn').on('click', function() {
$('.container').toggleClass('small-window')
});
}
}
$(document).ready(function() {
resize();
$(window).resize(resize);
});
body {
font-family: Arial, sans-serif;
}
h1 {
font-size: 20px;
}
.block-text {
display: block;
}
.container {
position: relative;
border: 1px solid;
min-height: 60px;
/* On larger screens */
display: flex;
align-items: center;
padding-left: 10px;
}
.btn {
position: absolute;
right: 10px;
display: none;
text-transform: uppercase;
}
@media (max-width: 700px) {
.btn {
display: block;
}
}
.small-window {
height: 120px !important;
}
span {
display: block;
margin-top: 8px;
}
<div class="container">
<h1>Resize Window</h1>
<button type="button" class="btn">Toggle height</button>
</div>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>