Created
March 19, 2023 12:04
-
-
Save vpberiya/d07fb33e0c7610bc08240c5f378a4f65 to your computer and use it in GitHub Desktop.
Image Zoom on mouse hover
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="container"> | |
<div class="product-img--main" data-scale="1" data-image="https://picsum.photos/1200/1200"> | |
</div> | |
<div class="product-img--main" data-scale="1.2" data-image=" https://picsum.photos/1200/1200"> | |
</div> | |
<div class="product-img--main" data-scale="1.6" data-image="https://picsum.photos/1200/1200"> | |
</div> | |
<div class="product-img--main" data-scale="2" data-image="https://picsum.photos/1200/1200"> | |
</div> | |
<div class="product-img--main" data-scale="2.2" data-image="https://picsum.photos/1200/1200"> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('.product-img--main') | |
// tile mouse actions | |
.on('mouseover', function(){ | |
$(this).children('.product-img--main__image').css({'transform': 'scale('+ $(this).attr('data-scale') +')'}); | |
}) | |
.on('mouseout', function(){ | |
$(this).children('.product-img--main__image').css({'transform': 'scale(1)'}); | |
}) | |
.on('mousemove', function(e){ | |
$(this).children('.product-img--main__image').css({'transform-origin': ((e.pageX - $(this).offset().left) / $(this).width()) * 100 + '% ' + ((e.pageY - $(this).offset().top) / $(this).height()) * 100 +'%'}); | |
}) | |
// tiles set up | |
.each(function(){ | |
$(this) | |
// add a image container | |
.append('<div class="product-img--main__image"></div>') | |
// set up a background image for each tile based on data-image attribute | |
.children('.product-img--main__image').css({'background-image': 'url('+ $(this).attr('data-image') +')'}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.product-img--main { | |
position: relative; | |
overflow: hidden; | |
/* margin-bottom: 30px; */ | |
width: 600px; | |
height: 600px; | |
float: left; | |
margin: 10px; | |
} | |
.product-img--main__image { | |
position: absolute; | |
top: 0; | |
left: 0; | |
width: 100%; | |
height: 100%; | |
background-position: center; | |
background-size: cover; | |
background-repeat: no-repeat; | |
-webkit-transition: -webkit-transform .5s ease-out; | |
transition: -webkit-transform .5s ease-out; | |
transition: transform .5s ease-out; | |
transition: transform .5s ease-out,-webkit-transform .5s ease-out; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment