在可滚动内容绝对div后面

时间:2016-12-02 09:38:49

标签: 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;}

请问有什么解决方案吗? :)

1 个答案:

答案 0 :(得分:1)

终于找到了解决方案。只需从您的div position: relative中删除.page-nav,然后将以下代码添加到此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>