javascript - 向下滚动时更改 div 高度

标签 javascript jquery

我有包含谷歌地图的 div 元素。 div 位于页面的一半,并且从中间开始(或多或少)。

当用户向下滚动时,它的高度是否可能会扩大(向上滚动时会缩小)?

我有一些脚本可以做到这一点,但它不是那么友好。

当前脚本是:

// 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"); ;
    }
});

最佳答案

在这里试试这个:

$(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");
    }
});

直到滚动到570, map 将始终保持在开始时的状态。之后它将顺利地跟随滚动。

关于javascript - 向下滚动时更改 div 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49873544/

相关文章:

javascript - 如何将选定的单选按钮值插入表格单元格

jquery - z-index 在右键单击时不起作用

javascript - Eternicode Bootstrap 日期选择器在触摸 Android Webkit< 534 后选择输入

javascript - Angular2 生命周期钩子(Hook)在加载内容时无法按预期工作

php - 如何添加 jReject - jQuery 浏览器拒绝到 Joomla 1.5 网站

javascript - 如何在 JavaScript 运行时、回发之前在单击时立即为光标设置动画

jquery - setInterval 函数可以自行停止吗?

javascript - React、contextApi 中 this.context 始终为空

javascript - 移动和桌面之间的响应式导航调整大小问题

javascript - 显示表格行的函数