如何在滚动窗口时滚动或移动固定内容

问题描述 投票:0回答:1

我正在使用物化CSS。[link

我想在滚动页面或滚动窗口时移动选择的内容。我正在尝试,但无法在示例代码下工作。有可能这样做吗?

http://jsfiddle.net/uj6p7o3a/

$(document).ready(function(){
    $('select').formSelect();
  });


 $(document).on('click','.select-wrapper .select-dropdown.dropdown-trigger',function(){
     var this_c = this.getAttribute('data-target');
     var lastScrollTop = 0;
     var count_scroll = 1;
     $(window).scroll(function (event) {
        var pos_s_c = parseInt($('#'+this_c).css("top").replace('px',''));
        var pos_s = $('select').position();
        var st = $(this).scrollTop();
        if (st > lastScrollTop){
       count_scroll++;
       st++;
      } else if (st < lastScrollTop){
       count_scroll--;
       st--;
       }
      lastScrollTop = st;
      var scroll_data = count_scroll;

      $('.select-wrapper .dropdown-content.select-dropdown').css({'argin-top':(lastScrollTop-st-scroll_data)-pos_s.top+pos_s_c+'px'});

    });   

   });
javascript jquery html css materialize
1个回答
0
投票

如果您希望选择下拉菜单内容在滚动时上下移动,请从代码中删除此CSS。

.select-wrapper .dropdown-content.select-dropdown {
  position: fixed;
}

这导致下拉菜单停留在页面上。您可以运行下面的堆栈片段以查看其运行情况。

$(document).ready(function() {
  $('select').formSelect();
});


$(document).on('click', '.select-wrapper .select-dropdown.dropdown-trigger', function() {
  var this_c = this.getAttribute('data-target');
  var lastScrollTop = 0;
  var count_scroll = 1;
  $(window).scroll(function(event) {
    var pos_s_c = parseInt($('#' + this_c).css("top").replace('px', ''));
    var pos_s = $('select').position();
    var st = $(this).scrollTop();
    if (st > lastScrollTop) {
      count_scroll++;
      st++;
    } else if (st < lastScrollTop) {
      count_scroll--;
      st--;
    }
    lastScrollTop = st;
    var scroll_data = count_scroll;

    $('.select-wrapper .dropdown-content.select-dropdown').css({
      'argin-top': (lastScrollTop - st - scroll_data) - pos_s.top + pos_s_c + 'px'
    });

  });

});
#div {
  height: 2000px;
  padding: 20px
}
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
</head>
<body>
<div id="div" class="col s10">
  Contrarytopopularbelief,LoremIpsumisnotsimplyrandomtext.It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the
  more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum"
  (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32. The
  standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions
  from the 1914 translation by H. Rackham. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact
  original form, accompanied by English versions from the 1914 translation by H. Rackham.
  <select>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
    <option value="10" selected="selected">10</option>
  </select>
</div>
</body>
</html>
© www.soinside.com 2019 - 2024. All rights reserved.