2013-09-28 83 views
1

有人能告訴我如何調整這個.js嗎?我希望它能夠在我的html中使用ID申請H1和H2。現在它只處理CSS背景圖像。下面是我使用的.js。我是一個JavaScript的初學者;)視差H1和H2

希望你們能幫助我!

(function($){ 
    var $window = $(window); 
    var windowHeight = $window.height(); 

$window.resize(function() { 
    windowHeight = $window.height(); 
}); 

$.fn.parallax = function(xpos, speedFactor, outerHeight) { 
    var $this = $(this); 
    var getHeight; 
    var firstTop; 
    var paddingTop = 0; 

    //get the starting position of each element to have parallax applied to it  
    $this.each(function(){ 
     firstTop = $this.offset().top; 
    }); 

    if (outerHeight) { 
     getHeight = function(jqo) { 
      return jqo.outerHeight(true); 
     }; 
    } else { 
     getHeight = function(jqo) { 
      return jqo.height(); 
     }; 
    } 

    // setup defaults if arguments aren't specified 
    if (arguments.length < 1 || xpos === null) xpos = "50%"; 
    if (arguments.length < 2 || speedFactor === null) speedFactor = 0.1; 
    if (arguments.length < 3 || outerHeight === null) outerHeight = true; 

    // function to be called whenever the window is scrolled or resized 
    function update(){ 
     var pos = $window.scrollTop();    

     $this.each(function(){ 
      var $element = $(this); 
      var top = $element.offset().top; 
      var height = getHeight($element); 

      // Check if totally above or totally below viewport 
      if (top + height < pos || top > pos + windowHeight) { 
       return; 
      } 

      $this.css('backgroundPosition', xpos + " " + Math.round((firstTop - pos) * speedFactor) + "px"); 

     }); 
    }  

    $window.bind('scroll', update).resize(update); 
    update(); 
}; 
})(jQuery); 

回答

0

它應該是background-position。你可以嘗試改變嗎?

$this.css('background-position', xpos + " " + Math.round((firstTop - pos) * speedFactor) + "px");