Yes it is possible, there are different ways, one of them is using Javascript/jQuery to know the dimensions and position of the element to be able to synchronize the scroll with these values.
Use .offset()
can know where the element begins and adding to it the .outerHeight()
knows where it ends. Then you need to know the value of scroll and this can get with .scroll()
. To finish, and once the scroll event is not cancellable, you have to rewrite the value of the scroll if it passes "the limits".
Code suggestion:
var ultimoScroll;
var dialogPosition = $('#seu_dialog').offset().top;
var dialogHeight = $('#seu_dialog').outerHeight();
$(window).on('scroll', function () {
var scroll = $(this).scrollTop();
if (scroll > dialogPosition + dialogHeight.outerHeight() || scroll < dialogPosition) $(window).scrollTop(ultimoScroll);
else ultimoScroll = scroll;
});
This is the skeleton you need. As you did not give any HTML/CSS/JS code I leave an example invented by me that with an adaptation of this code I mentioned above: http://jsfiddle.net/a825dnfm/
Hi Sergio, I adapted your code to my script and just fixed there
dialogHeight.outerHeight()
for onlydialogHeight
. His code worked as well as I wanted, but he is locking the scroll at the end of the div, IE, the div goes all the way to the top and hangs at the end of it. You’d have to crash when the div end is at the bottom of the screen, so you can’t climb any higher.– Ricardo
Of course, I was just going to make a calculation by decreasing the height of the window and that was it! I modified the
if
for:if (scroll > dialogPosition + ( dialogHeight - SC(window).height() )
, thank you!!– Ricardo
Sergio, can you tell me how to make this code work on mobile? Because the site is responsive and viewed on smartphones or tablets, this scroll scheme does not work.
– Ricardo
@Ricardo the code should work on mobile. I know that jQuery does not officially support mobile, hence jQuery mobile. If you have problems on mobile put an example with your code to test.
– Sergio
Ah tah. Don’t Linkei a jQuery mobile library. I’ll do it then. Thank you.
– Ricardo