Как прокрутить или переместить фиксированный контент при прокрутке окна - PullRequest
0 голосов
/ 08 апреля 2020

Я использую материализовать css. ( ссылка )

Я хочу переместить выбранный контент во время прокрутки страницы или прокрутки окна. Я пытаюсь, но не работает ниже пример кода. Есть ли возможность сделать это?

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'});

    });   

   });

1 Ответ

0 голосов
/ 09 апреля 2020

В вашем файле скрипта была опечатка.

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

Это было argin-top вместо margin-top

Ниже работает фрагмент кода. Фиксированный контент перемещается при прокрутке.

$(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({
      'margin-top': (lastScrollTop - st - scroll_data) - pos_s.top + pos_s_c + 'px'
    });

  });

});
#div {
  height: 2000px;
  background-color: #fff;
  padding: 20px
}

.select-wrapper .dropdown-content.select-dropdown {
  position: fixed;
}
<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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...