简体   繁体   English

向下滚动时更改div高度

[英]change div height when scrolling down

I have div element that contain google map. 我有包含谷歌地图的div元素。 The div is on the half of the page, and it's start from the middle (more or less). div位于页面的一半,它从中间(或多或少)开始。

Is it possible that the height of it will be expand when user scroll down ( and shrink when scroll up) ? 当用户向下滚动时,其高度是否有可能扩大(向上滚动时,其高度会缩小)?

I have some script that do it, but it's not so friendly. 我有一些脚本可以做到这一点,但是它并不那么友好。

and the current script is: 当前脚本是:

// change map size when scroll down
$(window).scroll(function() {    
    var scroll = $(window).scrollTop();

    if (scroll >= 450) {
        $("#map-warp").addClass("change-map-canvas");   // removed the . from class
        $("#map-warp").css("margin-top", "40px"); ;     // change margin from top for close infoWindow button
    } else {
        $("#map-warp").removeClass("change-map-canvas");  // removed the . from class
        $("#map-warp").css("margin-top", "10px"); ;
    }
});

Try this here: 在此尝试:

$(window).scroll(function() {    
    var scroll = $(window).scrollTop();
    var topMargin = 10 - $(window).scrollTop();

    if (scroll >= 570) {
        $("#map-warp").addClass("change-map-canvas"); 
        $("#map-warp").css("margin-top", "40px");
    } else {
        $("#map-warp").removeClass("change-map-canvas");
        $("#map-warp").css("margin-top", topMargin+"px");
    }
});

Until the scroll reaches 570, the map will always stay like in the beginning. 在滚动达到570之前,地图将始终像开始时一样停留。 After that it will smoothly follow the scroll. 之后,它将平稳地跟随滚动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM