为什么用鼠标滚轮滚动时背景会闪烁
Why the background is blinking if scrolled with mouse wheel?
我想创建我自己的小解决方案来操纵背景图像位置,但是如果我通过鼠标滚轮滚动,那么背景图像是"闪烁"的。实际上它是向上移动的,然后校正了位置,它看起来像在闪烁。
我该如何解决这个问题?
下面是我的HTML代码:
<section class="parallax">
<div class="parallax-item">
any text
</div>
<div class="parallax-img-container" id="img1" style="background: url('https://upload.wikimedia.org/wikipedia/commons/3/3e/Example_of_night_photography_at_The_Garden_of_Five_Senses,_New_Delhi.JPG') 0px 0px; background-size: cover; border: 1px solid #000;"></div>
<div class="parallax-item">
any other text
</div>
</section>
下面是jQuery代码:
$(function() {
// set up the items height to the screen height
var height = $(window).height();
$('.parallax-item').height(height).css({'background':'#fff'});
$('.parallax-img-container').height(height);
// catch scroll event
$(window).scroll(function() {
if ( isScrolledIntoView('#img1') ) {
$('#img1').css({'background-position':'0px '+parseInt( $(window).scrollTop() )+'px'});
}
});
});
下面是Scott Dowding对这个问题的回答,并做了一些修改来检测项目是否可见:
function isScrolledIntoView(elem) {
var $elem = $(elem);
var $window = $(window);
var docViewTop = $window.scrollTop();
var docViewBottom = docViewTop + $window.height();
var elemTop = $elem.offset().top;
var elemBottom = elemTop + $elem.height();
//return ((elemBottom > docViewBottom) || (elemTop < docViewTop));
return (((elemTop <= docViewBottom) && (elemBottom >= docViewBottom)) || ((elemTop < docViewTop) && (elemBottom >= docViewTop) ));
}
如果我在桌面电脑中通过滚动条滚动,背景图像的位置就会很好。但它开始"闪烁",如果我使用鼠标滚轮滚动。我该如何解决这个问题?
更新!这是在jsfiddle.net。
这似乎只在Internet Explorer中存在,这是我以前遇到过的问题。这段代码帮助了我;
<script>
$(document).ready(function() {
/* Internet explorer fixed background jitter fix */
if(navigator.userAgent.match(/Trident'/7'./)) {
$('body').on("mousewheel", function () {
event.preventDefault();
var wheelDelta = event.wheelDelta;
var currentScrollPosition = window.pageYOffset;
window.scrollTo(0, currentScrollPosition - wheelDelta);
});
}
});
</script>
哦,我应该提到(据我所知)这是因为ie的"平滑滚动"。如果你把它关掉,它就会像预期的那样工作。(引用http://answers.microsoft.com/en - us/ie/forum/ie11 iewindows8_1/choppy静态-背景-使用-光滑- scroll/8b53a32b db21 - 4 - fa3 a61d - 7732 f3fc217a?auth=1)
我找到了答案,并感谢Woopaa的答案开始思考正确的方式。
需要使用jQuery鼠标滚轮插件和simplr-smoothscroll插件的平滑滚动:
<script type="text/javascript" src="js/mousewheel/jquery.mousewheel.min.js"></script>
<script type="text/javascript" src="js/smoothscroll/simplr-smoothscroll.js"></script>
<script type="text/javascript">
$(function() {
// initialize the smooth scroll plugin
$.srSmoothscroll();
// set up the items height to the screen height
var height = $(window).height();
$('.parallax-item').height(height).css({'background':'#fff'});
$('.parallax-img-container').height(height);
// catch scroll event
$(window).scroll(function() {
if ( isScrolledIntoView('#img1') ) {
$('#img1').css({'background-position':'0px '+parseInt( $(window).scrollTop() )+'px'});
}
});
});
</script>
相关文章:
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- javascript样式的背景颜色在设置时闪烁.ASP.NET
- 刷新闪烁时的随机背景(汤博乐)
- 滚动的背景图像仍在闪烁
- skrollr背景图像在Firefox中闪烁
- iOS 8中滚动时的背景图像闪烁问题
- 更改元素父元素的背景颜色时停止在
上闪烁鼠标悬停?jQuery/JS. - 如何让这个Jquery闪烁的背景在几秒钟后停止
- 在Webkit和Edge(jQuery和vanillaJS)上滚动时生涩/闪烁的背景图像
- 如何将画布背景颜色设置为闪烁'n'秒
- 如何使按钮在单击时闪烁不同的背景颜色/
- Web:背景闪烁
- 为什么背景颜色会闪烁而不是平滑变化
- Jquery改变背景颜色悬停,不断闪烁
- Javascript背景颜色闪烁与褪色
- 如何防止白色'闪烁'对页面加载造成的背景图片加载延迟
- 为什么用鼠标滚轮滚动时背景会闪烁
- Javascript闪烁背景颜色:何时停止闪烁,何时开始闪烁
- 如何去除背景图像变化闪烁
- 闪烁的星星背景错误-HTML5画布和Javascript