[英]how to push a selected value into top of the scroll bar
我有一个垂直滚动条的下拉菜单。 我需要的是,如果我从下拉列表中选择任何文本,则该文本应位于滚动顶部的顶部。 防爆。 我的下拉菜单中有10个项目。 如果我选择第三个项目,则所选项目应在滚动条顶部可见。 我尝试使用以下代码。
$(document).ready(function(){
$("p").on("click", function(){
var scroll = $(document).scrollTop();
alert(scroll);
});
});
我试过了,不知道该怎么做。 帮我
与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>
带滚动
$(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.