简体   繁体   English

将垂直滚动转换为水平div位置jQuery

[英]Converting vertical scroll to horizontal div position jQuery

Been battling with this for a while. 一直在与这个斗争。 Seems simple enough but I seem to lack the logic required to get it to work. 看起来很简单,但我似乎缺乏使其工作所需的逻辑。

I want to convert the vertical .scrollTop position to a horizontal bar that represents the users vertical position in the document. 我想将垂直.scrollTop位置转换为代表用户在文档中垂直位置的水平条。

I think my math is terrible; 我认为我的数学太糟糕了。 am ready to be shot down in flames. 准备好在火焰中被击落。

var pos = $("#content").scrollTop();
var convert = (pos / 1024);

$(document).scroll(function() {
    $(".place").animate({
        left: '+=' + pos
    }, slow);
});​

Here's a fiddle of where I am so far. 这里有一个小提琴的,我至今在哪里。 The 'place' div doesn't want to move. 'place'div不想移动。

Start by converting the scroll position into a percentage 首先将滚动位置转换为百分比

 var s = $(window).scrollTop(),
    d = $(document).height(),
    c = $(window).height();
  var percent =  scrollPercent = (s / (d-c))

then get your current position by using that percentage for the width of the .placebar 然后使用该百分比作为.placebar的宽度来获取当前位置

var newPos = percent*1024;

if(newPos > 984) { //check to stop limit
   newPos = 984;
}

$("#place").stop().animate({
    left: newPos +"px"
});

Fiddle 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM