简体   繁体   English

使用Wheel JavaScript使用水平滚动创建扩展的DIV /画布

[英]Creating an Expanding DIV/Canvas with Horizontal Scrolling with Wheel JavaScript

Ok - here is what I am trying to do. 好的-这是我想要做的。 I was looking online for a cool timeline that I can purchase - allowing zoom in zoom out, posting of events on it, and so on. 我在网上寻找可以购买的很酷的时间表-允许放大,缩小,发布事件等等。 However, all the examples I found are either too expensive or just downright useless. 但是,我发现的所有示例太昂贵或完全没有用。

So, I have decided to create my own, but there are two elements that I am having trouble with. 因此,我决定创建自己的,但是我遇到了两个问题。

1) Converting the wheel scroll to left-right scrolling (so not up-down). 1)将滚轮滚动转换为左右滚动(因此不能上下滚动)。 I can't seem to find an easy and quick way to do this. 我似乎找不到简单快捷的方法来做到这一点。

But, more importantly.. 但是,更重要的是..

2) I need the area I will be showing the timeline on to automatically expand as I go about my scrolling. 2)我需要将要显示时间轴的区域,以便在滚动时自动扩展。 So, if I scroll down, it will add an "equivalent" area on the right, and down, on the left. 因此,如果我向下滚动,它将在右侧添加一个“等效”区域,在左侧添加一个“等效”区域。 So I was thinking like making an iFrame (already use these) and when you scroll it just adds more "timeline" on the left or the right, loads what ever it needs to load from the DB/list of events, and so on, ad infinitum, thus creating an ever-expanding list of blocks that are time-sized. 因此,我当时想制作一个iFrame(已经使用了它们),并且在滚动时仅在左侧或右侧添加了更多“时间轴”,从数据库/事件列表中加载了需要加载的内容,依此类推,无限的广告,因此创建了一个不断扩大的时空块列表。

If I can do the two things above, then I am set - the rest (loading/positioning) I can figure out - just these two things are eluding my imagination and ability to find an answer. 如果我能做以上两件事,那么我就定了-剩下的(装载/定位)我可以弄清楚了-只是这两件事使我的想像力和寻找答案的能力消失了。

Basically you need a horizontal infinite scroll script. 基本上,您需要一个水平的无限滚动脚本。

Take this plugin I wrote: 拿我写的这个插件:

 $.fn.hScroll = function( options )
 {
  function scroll( obj, e )
  {
    var evt = e.originalEvent;
    var direction = evt.detail ? evt.detail * (-120) : evt.wheelDelta;

    if( direction > 0)
    {
        direction =  $(obj).scrollLeft() - 120;
    }
    else
    {
        direction = $(obj).scrollLeft() + 120;
    }

    $(obj).scrollLeft( direction );

    e.preventDefault();
  }

  $(this).width( $(this).find('div').width() );

  $(this).bind('DOMMouseScroll mousewheel', function( e )
  {
      scroll( this, e );
  });
}

Initialize it with: 初始化它:

  $('body').hScroll();

Makes your website a horizontally scrollable website. 使您的网站成为可水平滚动的网站。

Your content div must be wider than your body (ex. 3000px). 您的内容div必须大于您的身体(例如3000px)。

As for the infinite scrolling effect you pretty much gotta do that your self because I can't know what kind of data you'll input. 至于无限滚动效果,您几乎必须自行完成,因为我不知道您将输入哪种数据。 But I'll explain. 但我会解释。

Your children elements in the content div must be floated to left. 内容div中的子元素必须向左浮动。 (every new appended div will not go to new line). (每个新附加的div都不会转到新行)。

Set an interval to check if the user's scrollLeft position is near the end of the content (just like pinterest and similar site). 设置时间间隔以检查用户的scrollLeft位置是否接近内容的结尾(就像pinterest和类似的网站一样)。

    function loadNewData(){ /* Your search for data and update here. */ }
    setInterval('loadNewData', 500);

search for new data according to your last one with AJAX. 使用AJAX根据您的上一个搜索新数据。 When you get new data, append it into your content div (in a div that's floated left, as I wrote previously), and mark it as your last item. 当您获得新数据时,将其追加到内容div中(如我先前所写,在浮动的div中),并将其标记为最后一项。

Maybe you could use your ID to mark the last item on it's div. 也许您可以使用ID来标记div上的最后一项。

   <div data-id="467" class="item"> // your data here  </div>

You can fetch it with 你可以用

   $('.item:last').attr('data-id');

with jQuery. jQuery。

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

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