Javascript 禁用移动设备上的视差
我在一个有着奇特视差滚动背景的网站上工作,并遵循Mohiuddin Parekh(可用)的教程 这是我的javascript:Javascript 禁用移动设备上的视差,javascript,jquery,css,mobile,parallax,Javascript,Jquery,Css,Mobile,Parallax,我在一个有着奇特视差滚动背景的网站上工作,并遵循Mohiuddin Parekh(可用)的教程 这是我的javascript: $(document).ready(function(){ // Cache the Window object $window = $(window); $('section[data-type="background"]').each(function(){ var $bgobj = $(this); // assigning the object $(
$(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
});
});
这很有效。现在我想做的是,如果使用移动设备(最大宽度:768px)查看站点,则不执行javascript。不幸的是,我不太清楚如何实现这一点,非常感谢您的帮助:)页面启动时会触发document ready(文档准备就绪),当有人操纵窗口时会调整窗口大小
$( 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
});
}
});
获取窗口宽度并用if-like-so,if(_-window\u-width>770){your scroll code}(_-window\u-width是保存$(window.width())的变量该死,太快了!谢谢:)