移動

2014-04-01 43 views
2

禁用視差我的工作與花哨的視差滾動背景在網站上,並隨後從Mohiuddin帕雷克(可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 

});  

}); 

這個偉大的工程。現在我想做些什麼,如果是該網站與移動設備觀看不執行JavaScript(最大寬度:768px)。不幸的是,我不太清楚如何實現這一點,任何幫助表示讚賞:)

+2

獲取窗口寬度,敷滾動在如果像這樣,如果(_window_width> 770){您的滾動代碼}(_window_width在於保持$(窗口中的變量).WIDTH()) –

+0

https://developer.mozilla.org/en-US/docs /Web/API/Window.matchMedia – enapupe

回答

4

文件準備好時觸發頁開始,和窗口大小調整操作,當有人操縱窗口

$(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 

});  
} 
}); 
+0

媽的,這是快!謝謝 :) – Paranoia