$(window).scroll(function() { }); window的滚动事件不触发以及offset().top数据乱的原因?

当CSS中html和body的高度设置为100%时,$(window).scroll()事件可能无法正确触发,导致滚动监听失效。解决办法是在CSS中为html元素添加height: auto,同时保持body的overflow-y: scroll属性,以确保滚动事件能正常工作。

$(window).scroll(function() { }); window的滚动事件不触发以及offset().top数据乱的原因?

如果你的css中:html,body的高度样式如果设置为100%,那么$(window).scroll方法将检测不到正确的滚出高度(0),导致滚动监听事件失效。

$(window).scroll() 方法不能触发

这个是我在项目中遇到**,$(window).scroll()**当我在使用这个事件方法的时候竟然不能响应,很是郁闷,后来才发现是css样式影响到了,废话少说,直接上代码
这个是我在js中使用的

$(window).scroll(function (event) {
    console.log(event);
})

修改之前的css样式(滚动事件不触发的错误写法):

body {
    width: 100%;
    overflow-y:scroll;
}

修改之后的css样式 只是比之前多加了一个html的样式 就可以响应了(滚动事件正常触发)

html {
    width: 100%;
    height:auto;     // 主要是这个height:auto
    overflow-x: hidden;
}

body {
    width: 100%;
    overflow-y:scroll;
}
$(document).ready(function(){ let currentIndex = 0; const $items = $('.carousel-inner img'); const totalItems = $items.length; let autoPlay = setInterval(nextSlide, 3000); // 初始化指示点 function initDots() { for(let i=0; i<totalItems; i++){ // $('.carousel-dots').append('<div class="dot"></div> '); // $('.carousel-dots').append($('<div>').text(i+1).addClass('dot').append($('<span>').addClass('heng'))); // var $span = $('<span>').text(123); if (i + 1 < totalItems) { $('.carousel-dots').append($('<div>').text((i+1).toString().padStart(2, '0')).addClass('dot').append($('<span>').addClass('heng'))); }else{ $('.carousel-dots').append($('<div>').text((i+1).toString().padStart(2, '0')).addClass('dot')); } } $('.dot').eq(0).addClass('active'); } initDots(); // 切换幻灯片 function showSlide(index) { $items.removeClass('active').eq(index).addClass('active'); $('.dot').removeClass('active').eq(index).addClass('active'); } // 下一张 function nextSlide() { currentIndex = (currentIndex + 1) % totalItems; showSlide(currentIndex); } // 上一张 function prevSlide() { currentIndex = (currentIndex - 1 + totalItems) % totalItems; showSlide(currentIndex); } // 事件绑定 $('.next').click(function(){ clearInterval(autoPlay); nextSlide(); autoPlay = setInterval(nextSlide, 3000); }); $('.prev').click(prevSlide); $('.carousel-dots').on('click', '.dot', function(){ const index = $(this).index(); currentIndex = index; showSlide(currentIndex); }); // 悬停暂停 $('.carousel').hover( () => clearInterval(autoPlay), () => autoPlay = setInterval(nextSlide, 3000) ); // //平移动画效果 // $(document).ready(function() { // // 方法1:使用animate()方法 // $(".jianji").animate({ // left: "720px" // 移动到距离左侧250px的位置 // }, 1000 ,); // 动画持续时间2000ms(2秒) // // 方法2:使用CSS transform(更流畅的硬件加速方案) // // $("#movingObject").css({ // // transform: "translateX(600px)", // // transition: "2s" // // }); // }); // function qiehuan() { // // 方法1:使用animate()方法 // $(".jianji").animate({ // left: "720px" // 移动到距离左侧250px的位置 // }, 1000 ,); // 动画持续时间2000ms(2秒) // }; // qiehuan() // $(document).ready(function() { // var animationTriggered = false; // $(window).scroll(function() { // var scrollPosition = $(window).scrollTop(); // var triggerPosition = $(".jianji").offset().top - $(window).height() +50; // if (scrollPosition >= triggerPosition && !animationTriggered) { // animationTriggered = true; // $(".jianji") // .animate({ left: "300px" }, 800) // 向右滑动 // .animate({ height: "200px" }, 600); // 再调整高度 // } // }); // }); var animationTriggered = false; $(window).scroll(function() { var scrollPosition = $(window).scrollTop(); var triggerPosition = $(".jianji").offset().top - $(window).height() +5200; if (scrollPosition >= triggerPosition && !animationTriggered) { animationTriggered = true; $(".jianji") .animate({ left: "750px" }, 800) // 向右滑动 // .animate({ height: "200px" }, 600); // 再调整高度 } }); });让这个轮播图的图片切换效果是左右滑动
04-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值