[英]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.