I made a responsive decision (I think so) using JQ. check it below or in jsFiddle
First, I cached all the necessary selectors for cleaner and more concise code.
-20 is due to div { margin-top:20px } `there I calculated the upper offset of both divs relative to the document, and then got the width and height of the small div
in the click function, I first got the top offset of the image, so I could compare this to C # small offset.
so if the distance to the top is less than # a small distance to the top, this means that img is in the #large div, and therefore I move it with transform: translate, giving it the Y axis value equal to TOP offset #small Div, therefore img offset.top (iOffset) becomes equal to #small offset.top (sOffset)
also adds the width and height of the #small div to the image
else (if iOffset is = or greater than sOffset), then this means that the image is not in a large div, so I need to translate it back to the offset #large div and add the width: 100% and height: 100%
I hope I understood this correctly and correctly explained. let me know if that helps
var Large = $("#large"), Small = $("#small"), lOffset = $(Large).offset().top - 20 + 'px', sOffset = $(Small).offset().top - 20 + 'px', sWidth = $(Small).width(), sHeight = $(Small).height() $(document).on("click", "img", function() { var iOffset = $(this).offset().top + 'px' if (iOffset < sOffset) { $(this).css('transform', 'translate(0,' + sOffset + ')') .width(sWidth).height(sHeight) } else { $(this).css('transform', 'translate(0,' + lOffset + ')') .width("100%").height("100%") } })
div { margin: 20px; padding: 10px; } #large { width: 600px; height: 400px; background-color: gray; } #small { width: 120px; height: 90px; background-color: orange; } img { width: 100%; height: 100%; transition: 5s; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="large"> <img src="https://ak.picdn.net/assets/cms/5bb580387901a83212e3028ab5a2fa8fb1153d7b-img_offset_2x.jpg" /> </div> <div id="small"> </div>
source share