gpt4 book ai didi

javascript - 在移动设备上禁用视差

转载 作者:行者123 更新时间:2023-11-29 17:09:10 25 4
gpt4 key购买 nike

我正在一个具有花式视差滚动背景的网站上工作,并遵循了 Mohiuddin Parekh 的教程(可用 here)

这是我的javascript:

$(document).ready(function(){
// Cache the Window object
$window = $(window);

$('section[data-type="background"]').each(function(){
var $bgobj = $(this); // assigning the object

$(window).scroll(function() {

// Scroll the background at var speed
// the yPos is a negative value because we're scrolling it UP!
var yPos = -( ($window.scrollTop() - $bgobj.offset().top) / $bgobj.data('speed'));

// Put together our final background position
var coords = '50% '+ yPos + 'px';

// Move the background
$bgobj.css({ backgroundPosition: coords });

}); // window scroll Ends

});

});

这很好用。现在我想做的是,如果使用移动设备查看网站(最大宽度:768 像素),则不执行 javascript。不幸的是,我不太确定如何实现这一目标,我们将不胜感激:)

最佳答案

文档就绪在页面启动时触发,当有人操作窗口时窗口调整大小

$( window ).resize(function() {
$window = $(window);
if( $window .width() > 800){

$('section[data-type="background"]').each(function(){
var $bgobj = $(this); // assigning the object

$(window).scroll(function() {

// Scroll the background at var speed
// the yPos is a negative value because we're scrolling it UP!
var yPos = -( ($window.scrollTop() - $bgobj.offset().top) / $bgobj.data('speed'));

// Put together our final background position
var coords = '50% '+ yPos + 'px';

// Move the background
$bgobj.css({ backgroundPosition: coords });

}); // window scroll Ends

});
}
});



$(document).ready(function(){
$window = $(window);
if( $window.width() > 800){
// Cache the Window object

$('section[data-type="background"]').each(function(){
var $bgobj = $(this); // assigning the object

$(window).scroll(function() {

// Scroll the background at var speed
// the yPos is a negative value because we're scrolling it UP!
var yPos = -( ($window.scrollTop() - $bgobj.offset().top) / $bgobj.data('speed'));

// Put together our final background position
var coords = '50% '+ yPos + 'px';

// Move the background
$bgobj.css({ backgroundPosition: coords });

}); // window scroll Ends

});
}
});

关于javascript - 在移动设备上禁用视差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22785473/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com