是否可以创建一个水平和垂直可滚动的网站?

时间:2016-04-18 03:54:45

标签: javascript jquery css horizontal-scrolling vertical-scrolling

我正在尝试制作一个实验叙事网站,讲述一个页面中的两个不同故事。该网站目前只是水平滚动,我只有一个大的固定图像顶部的图像,中间有一个大洞。我最初在水平滚动的末尾有一个可点击的图像,所以它链接到另一个故事,但我想知道我是否可以有水平滚动和垂直滚动,以便它同时显示不同的图像?就像用户垂直滚动一样,水平滚动停止;当用户水平滚动时,垂直滚动停止?谢谢。

here's a reference image here

1 个答案:

答案 0 :(得分:2)

是。一种方法是使用jQuery捕获mousewheel事件来触发自定义滚动功能,同时使用overflow: hidden隐藏默认滚动条以避免任何奇怪的行为。因此,它实际上是关于创建一个受控环境,其中更改发生在事件句柄而不是本机浏览器"滚动"。

看看我的基本示例:view on codepen



// jQuery Next or First / Prev or Last plugin

$.fn.nextOrFirst = function(selector){
    var next = this.next(selector);
    return (next.length) ? next : this.prevAll(selector).last();
};

$.fn.prevOrLast = function(selector){
    var prev = this.prev(selector);
    return (prev.length) ? prev : this.nextAll(selector).last();
};

// Scroll Functions

function scrollSection(parent, dir) {
	var active = "active",
  		section = parent.find("."+active);
  if (dir == "prev") {
    section.removeClass(active).prevOrLast().addClass(active);
  } else {
    section.removeClass(active).nextOrFirst().addClass(active);
  }
}

// Bind Scroll function to mouse wheel event

$('#vertical, #horizontal').on('mousewheel wheel', function(e){
  if (e.originalEvent.wheelDelta /120 > 0) { // scroll up event
    scrollSection($(this), "prev");
  } else { // scroll down event
    scrollSection($(this));
  }
});

html, body, #vertical {
  color: #FFF; margin: 0; padding: 0;
  height: 100%; overflow: hidden; }
  
#horizontal {
  height: 70vh; width: 70vh;
  position: fixed; margin: auto;
  top: 0; left: 0; right: 0; bottom: 0;
  display: flex;
  border-radius: 100%;
  overflow: hidden; }

section {
  height: 0%;
  box-sizing: border-box;
  transition: 1s;
  overflow: hidden; }

#horizontal section {
  width: 100%; height: 100%;
  min-width:0%;
  text-align: center; }

.inner { padding: 3em; }

#vertical section.active { height: 100%; }

#horizontal section.active { min-width: 100%; }

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="horizontal">
  <section class="active" style="background:Black"><div class="inner">Horizontal 1</div></section>
  <section style="background:BurlyWood"><div class="inner">Horizontal 2</div></section>
  <section style="background:MediumSlateBlue"><div class="inner">Horizontal 3</div></section>
</div>
<div id="vertical">
  <section class="active" style="background:SteelBlue"><div class="inner">Vertical 1</div></section>
  <section style="background:DarkSlateBlue"><div class="inner">Vertical 2</div></section>
  <section style="background:HotPink"><div class="inner">Vertical 3</div></section>
</div>
&#13;
&#13;
&#13;

你甚至可以让它同时滑动两个方框:

&#13;
&#13;
// jQuery Next or First / Prev or Last plugin

$.fn.nextOrFirst = function(selector){
    var next = this.next(selector);
    return (next.length) ? next : this.prevAll(selector).last();
};

$.fn.prevOrLast = function(selector){
    var prev = this.prev(selector);
    return (prev.length) ? prev : this.nextAll(selector).last();
};

// Scroll Functions

function scrollSection(parent, dir) {
	var active = "active",
  		section = parent.find("."+active);
  if (dir == "prev") {
    section.removeClass(active).prevOrLast().addClass(active);
  } else {
    section.removeClass(active).nextOrFirst().addClass(active);
  }
}

// Bind Scroll function to mouse wheel event

$('body').on('mousewheel wheel', function(e){
  if (e.originalEvent.wheelDelta /120 > 0) { // scroll up event
    scrollSection( $('#vertical'), "prev");
    scrollSection( $('#horizontal'), "prev");
  } else { // scroll down event
    scrollSection( $('#vertical') );
    scrollSection( $('#horizontal') );
  }
});
&#13;
html, body, #vertical {
  color: #FFF; margin: 0; padding: 0;
  height: 100%; overflow: hidden; }
  
#horizontal {
  height: 70vh; width: 70vh;
  position: fixed; margin: auto;
  top: 0; left: 0; right: 0; bottom: 0;
  display: flex;
  border-radius: 100%;
  overflow: hidden; }

section {
  height: 0%;
  box-sizing: border-box;
  transition: 1s;
  overflow: hidden; }

#horizontal section {
  width: 100%; height: 100%;
  min-width:0%;
  text-align: center; }

.inner { padding: 3em; }

#vertical section.active { height: 100%; }

#horizontal section.active { min-width: 100%; }
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="horizontal">
  <section class="active" style="background:Black"><div class="inner">Horizontal 1</div></section>
  <section style="background:BurlyWood"><div class="inner">Horizontal 2</div></section>
  <section style="background:MediumSlateBlue"><div class="inner">Horizontal 3</div></section>
</div>
<div id="vertical">
  <section class="active" style="background:SteelBlue"><div class="inner">Vertical 1</div></section>
  <section style="background:DarkSlateBlue"><div class="inner">Vertical 2</div></section>
  <section style="background:HotPink"><div class="inner">Vertical 3</div></section>
</div>
&#13;
&#13;
&#13;