Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何保持快速滚动div在同一位置,独立于屏幕大小?_Javascript_Position_Viewport_Parallax_Responsiveness - Fatal编程技术网

Javascript 如何保持快速滚动div在同一位置,独立于屏幕大小?

Javascript 如何保持快速滚动div在同一位置,独立于屏幕大小?,javascript,position,viewport,parallax,responsiveness,Javascript,Position,Viewport,Parallax,Responsiveness,为了使不同的数据滚动速度属性具有不同的滚动速度,我使用以下js: $.fn.moveIt = function(){ var $window = $(window); var instances = []; $(this).each(function(){ instances.push(new moveItItem($(this))); }); window.onscroll = function(){ var scrollTop = $window.sc

为了使不同的数据滚动速度属性具有不同的滚动速度,我使用以下js:

$.fn.moveIt = function(){
  var $window = $(window);
  var instances = [];

  $(this).each(function(){
    instances.push(new moveItItem($(this)));
  });

  window.onscroll = function(){
    var scrollTop = $window.scrollTop();
    instances.forEach(function(inst){
      inst.update(scrollTop);
    });
  }
}

var moveItItem = function(el){
  this.el = $(el);
  this.speed = this.el.attr('data-scroll-speed');
};

moveItItem.prototype.update = function(scrollTop){
  var pos = scrollTop / this.speed;
  this.el.css('transform', 'translateY(' + -pos + 'px)');
};

$(function(){
  $('[data-scroll-speed]').moveIt();
});
我的问题是,如果我调整浏览器窗口的大小,图像的垂直位置(我在其上使用了此js,而主内容具有默认速度)会发生变化,即图像显示在主内容的不同区域;我怎样才能使它们保持与屏幕大小无关的准确位置

主要内容已经完全响应(字体大小在vw中指定,更快的滚动图像根据屏幕宽度调整大小,等等)