Jquery自定义滚动与Rails .each循环

时间:2017-05-23 03:24:00

标签: javascript jquery css ruby-on-rails ruby-on-rails-5

尝试创建一个自定义滚动条,允许我向上和向下滚动所有使用.each循环填充的框。我能够显示灰色滚动条但蓝色滑块不显示。我在下面列出了所有相关代码。

show.html.erb

<div class="containermessanger">
  <div class="scrollBar">
      <div class="slider"></div>
  </div>
  <div class="scroll">
    <div class="content">
      <% @chatroomall.each do |chatroom|%>
        <div class="boxmessenger">
          <div class="row">
            <div class="col-md-2">
              <% if chatroom.messages.empty? %>
                No messages in this chatroom
              <% else %>
                <%= image_tag chatroom.messages.last.user.avatar.url(:thumb), id: "round-image-50"  %>
              <% end %>
            </div>
            <div class="col-md-8">
              <%= chatroom.name %>
            </div>
            <div class="col-md-2">                                  
              <%= chatroom.messages.last(1).pluck(:created_at) %>
              <br>                            
              <li class="btn-group" id="profilenavbig">
                <a class="dropdown-toggle" type="button" data-behavior="notifications-link" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" style="margin-left: 0%;">
                  <i class="fa fa-cog" aria-hidden="true"></i>                            
                </a>         
                <ul class="dropdown-menu">
                  <li> 
                    <i class="fa fa-trash" aria-hidden="true"></i>&nbspDelete                   
                  </li>
                  <li role="separator" class="divider">
                  </li>                                           
                  <li>
                    Report Spam<br>or Abuse 
                  </li>                                                                   
                </ul>
              </li>
            </div>
          </div>
          <div class="row">
            <div class="col-md-1">
            </div>
            <div class="col-md-11">                                                 
              <%= chatroom.messages.last(1).pluck(:body) %>                           
            </div>
          </div>                      
        </div>
      <% end %>
    </div>
  </div>
</div>

application.scss

.containermessanger{
  overflow:hidden;
  margin: 0;
  height: 500px;
  position:relative;
}
.scrollBar{
  background: #49505a;
  position: absolute;
  right: 9px;
  width: 5px;
  height: 100%;
  z-index: 1;
  top:0;
}
.slider{
  background: #5EAEE3;
  width: 20px;
  border-radius:10px;
  left:-7px;
}

.scroll{
  height: 100%;
  overflow: hidden;
}

messengerscroll.js

$(document).ready(function(){
  $bHeight = $(".content").height();
  $sHeight = $('.scrollBar').height();
  $sliderHeight = $sHeight/$bHeight*100;
  $('.slider').height($sliderHeight+'%');
  $('.slider').draggable({
    containment:'parent',
    axis:'y',
    drag:function(){
      $pos = $('.slider').position().top;
      $ScrollPercent = $pos/$sHeight*100;
      $ScrollPx = $ScrollPercent/100*$bHeight;
      $('.scroll').scrollTop($ScrollPx);
    }
  })
});

Chrome开发者 - HTML元素页 enter image description here

Chrome开发者 - 控制台 enter image description here

1 个答案:

答案 0 :(得分:0)

所以在@jvillian的帮助下,我能够破解一个有效的解决方案。解决这个问题的解决方案有两个。

第1步。 切换自: $ bHeight = $(“。content”)。height(); 至: $ bHeight = $(“。containermessanger”)。height();

第2步。 修改方程式: $ sliderHeight = $ sHeight / $ bHeight * 100; 至: $ sliderHeight = $ sHeight / $ bHeight * 20; enter image description here