html - 在可滚动内容上绝对 div 跟随

标签 html css scroll css-position absolute

我正在制作一个带有滚动控件的响应式菜单。我有一个小问题,我的渐变和控件跟随,滚动时,你可以在我的 JSFiddle 中看到它.

我的菜单控件有我的 CSS

#page .page-nav .controls{}

#page .page-nav .controls:after{
    display: block;
    content: none;
    width: 40%;
    height: 50px;
    position: absolute;
    top: 0;
    right: 0;
    background: -webkit-linear-gradient(left ,transparent 50px, red);
    z-index: 100;
}

#page .page-nav span {
    background-image: url("http://www.hotel-hlosnarcisos.com/joomla/components/com_jhotelreservation/assets/t/img/arrow_right.gif");
    background-repeat: no-repeat;
    background-size: 14px;
    height: 16px;
    width: 16px;
    display: none;
    top: 18px;
    position: absolute;
    z-index: 9999;
    cursor: pointer;
}

#page .page-nav span.previous {transform: rotate(180deg);left: 0;top: 11px;}

#page .page-nav span.next {transform: rotate(360deg);right: 0;top: 9px;}

请问有什么解决办法吗? :)

最佳答案

终于找到解决办法了。只需从您的 div .page-nav 中删除 position: relative 并将以下代码也添加到此 div

background: white -webkit-linear-gradient(left, transparent 50px, red);
background-size: 40% 100%;
background-repeat: no-repeat;
background-position: right;

现在您不再需要 .controls:after

$('<span class="previous"></span>').prependTo("#page .page-nav .menu-controls");
$('<span class="next"></span>').appendTo("#page .page-nav .menu-controls");

$(".previous").click(function() {
  var posLeft = $(".page-nav").scrollLeft();
  $(".page-nav").animate({
    scrollLeft: posLeft - 200
  }, 500);

});
$(".next").click(function() {
  var posLeft = $(".page-nav").scrollLeft();
  $(".page-nav").animate({
    scrollLeft: posLeft + 200
  }, 500);
});
@import url('https://fonts.googleapis.com/css?family=Roboto:100');
a {
  text-decoration: none;
}

#page {
  position: relative;
  font-family: "Roboto";
  font-weight: 100;
  font-size: 13px;
  position: relative;
}

#page .container {
  width: 100%;
  height: 100px;
  margin-top: 20px;
  background: white;
}

#page .page-nav {
  background: white -webkit-linear-gradient(left, transparent 50px, red);
  background-size: 40% 100%;
  background-repeat: no-repeat;
  background-position: right;
}

#page .page-nav::-webkit-scrollbar {
  -webkit-appearance: none;
  background: transparent;
  width: 0;
  height: 0;
}

#page .page-nav .items {
  display: table;
  margin: 0 auto;
}

#page .page-nav .menu-controls {}

#page .page-nav .controls {}

#page .page-nav .item {
  display: inline-block;
}

#page .page-nav .item a {
  color: #333333;
  display: block;
  padding: 6px 15px;
}

#page .page-nav .item.current a {
  color: #0099ff;
}

#page .page-nav span {
  background-image: url("http://www.hotel-hlosnarcisos.com/joomla/components/com_jhotelreservation/assets/t/img/arrow_right.gif");
  background-repeat: no-repeat;
  background-size: 14px;
  height: 16px;
  width: 16px;
  display: none;
  top: 18px;
  position: absolute;
  z-index: 9999;
  cursor: pointer;
}

#page .page-nav span.previous {
  transform: rotate(180deg);
  left: 0;
  top: 11px;
}

#page .page-nav span.next {
  transform: rotate(360deg);
  right: 0;
  top: 9px;
}

@media screen and (max-width: 864px) {
  #page .page-nav {
    border: 1px solid #dedede;
    border-left: none;
    border-right: none;
    white-space: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    -ms-overflow-style: -ms-autohiding-scrollbar;
    padding-left: 15px;
    padding-right: 25px
  }
  #page .page-nav .items {
    padding-right: 250px
  }
  #page .page-nav .controls:after {
    content: '';
  }
  #page .page-nav .item a {
    padding: 10px 15px;
    font-size: 13px
  }
  #page .page-nav span {
    display: inline-block;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="page">
  <div class="page-nav">
    <div class="items">
      <div class="item"><a href="">Menu item 1</a>
      </div>
      <div class="item"><a href="">Menu item 2</a>
      </div>
      <div class="item"><a href="">Menu item 3</a>
      </div>
      <div class="item"><a href="">Menu item 4</a>
      </div>
      <div class="item"><a href="">Menu item 5</a>
      </div>
      <div class="item current"><a href="">Menu item 6</a>
      </div>
      <div class="item"><a href="">Menu item 7</a>
      </div>
      <div class="item"><a href="">Menu item 8</a>
      </div>
      <div class="item"><a href="">Menu item 9</a>
      </div>
    </div>

    <div class="menu-controls">
      <div class="controls">
      </div>
    </div>
  </div>

  <div class="container">
    PRESS ON ARROWS OR HOLD SHIFT AND SCROLL MENU AND SEE, HOW RED GRADIENT AND ARROWS FOLLOWS THE SCROLL
  </div>
</div>

关于html - 在可滚动内容上绝对 div 跟随,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40928952/

相关文章:

html - 将一个标签的 css 样式用于其他标签?

jquery - 使用 jquery 在按钮单击时上下滚动 div

extjs - 面板内的面板 : autoScroll not working

从左到右阅读的 CSS 表

JQuery窗口滚动事件?

javascript - 我怎样才能实现这个水平搜索结果栏?

html - CSS 换行 : make second line bigger or specify word amount per line?

html - 空 xml 元素混淆 - HTML

html - 将不同的段落放在html5中的3张图片下,使用一个类

css - 使用 fontsquirrel 调整字体大小以匹配 Arial