简体   繁体   English

如何将选定的值推入滚动条的顶部

[英]how to push a selected value into top of the scroll bar

I have a dropdown with vertical scroll bar. 我有一个垂直滚动条的下拉菜单。 What i need is if i select any text from a drop down, it should be on the top of the scroll top. 我需要的是,如果我从下拉列表中选择任何文本,则该文本应位于滚动顶部的顶部。 Ex. 防爆。 I have 10 items in dropdown. 我的下拉菜单中有10个项目。 If i select 3rd item, the selected item should be visible on the top of scroll bar. 如果我选择第三个项目,则所选项目应在滚动条顶部可见。 I have try with the following code. 我尝试使用以下代码。

$(document).ready(function(){
    $("p").on("click", function(){
       var scroll = $(document).scrollTop();
       alert(scroll);
    });
});

Just I have try this, don't know how to do this. 我试过了,不知道该怎么做。 Help me 帮我

With prependTo 与prependTo

 $(document).ready(function(){ $("p").on("click", function(){ $(this).prependTo(".nano") }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <head> <body> <div class="container"> <div class="nano has-scrollbar"> <p>jerrin</p> <p>stephen</p> <p>augustin</p> <p>yeswanth</p> <p>irfan</p> <p>aashik</p> <p>dinesh</p> <p>zaid</p> <p>mari</p> <p>aravinth</p> <p>asf</p> <p>qwe</p> <p>ewq</p> <p>fhg</p> <p>uyt</p> <p>rty</p> <p>rty</p> <p>rty</p> </div> </div> </body> </html> 

With scroll 带滚动

 $(document).ready(function(){ $("p").on("click", function(){ $('html, body').animate({ scrollTop: $(this).offset().top },100); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <head> <body> <div class="container"> <div class="nano has-scrollbar"> <p>jerrin</p> <p>stephen</p> <p>augustin</p> <p>yeswanth</p> <p>irfan</p> <p>aashik</p> <p>dinesh</p> <p>zaid</p> <p>mari</p> <p>aravinth</p> <p>asf</p> <p>qwe</p> <p>ewq</p> <p>fhg</p> <p>uyt</p> <p>rty</p> <p>rty</p> <p>rty</p> </div> </div> </body> </html> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM