我有一个搜索框和一个div列表,填充了我项目中数据库中的数据。我的想法是,当我在搜索框上输入内容时,它将滚动列表中的特定数据。感谢,并有一个愉快的一天!
以下是 JSFIDDLE
我已编辑此问题并将此脚本添加到我的项目中,但它仍然没有滚动。
$(document).ready(function() {
$('.search-field').on('keyup blur change', function() {
var text_s = $(this).val();
$("li#dirlist").removeClass("highlight");
if (text_s.length > 0){
$("li#dirlist").each(function(){
var li_value = $(this).text();
if (li_value.indexOf(text_s) >= 0){
$(this).addClass("highlight");
var x = getOffset( document.getElementById(this.id) ).left;
}
});
}
});
});
function getOffset( el ) {
var _x = 0;
var _y = 0;
while( el && !isNaN( el.offsetLeft ) && !isNaN( el.offsetTop ) ) {
_x += el.offsetLeft - el.scrollLeft;
_y += el.offsetTop - el.scrollTop;
el = el.offsetParent;
}
window.scrollTo(_x,_y);
return { top: _y, left: _x };
}
答案 0 :(得分:1)
这是你想要的
JsFiddle: http://jsfiddle.net/q52Fc/1/
<强> HTML:强>
<div class="container">
<div class="row-fluid">
<div class="span12">
<div class="green-bar">
<h4><center>Directory</center></h4>
</div>
</div>
</div>
</br>
</br>
<div class="span4">
<div id="name-dir">
<div class="row">
<div class="control-group">
<div class="controls">
<div class="input-prepend"> <span class="add-on"><i class="icon-search"></i></span>
<input class="block search-field" id="inputIcon" type="text" placeholder="Search">
</div>
</div>
</div>
</div>
</br>
<div class="contact-list">
<div class="contact-gray-bar">A</div>
<ul class="contact">
<!-- insert records here -->
<li>Apple</li>
<li>Amethyst</li>
<li>Aratilis</li>
</ul>
<div class="contact-gray-bar">B</div>
<ul class="contact">
<!-- insert records here -->
<li>Banana</li>
</ul>
<div class="contact-gray-bar">C</div>
<ul class="contact">
<!-- insert records here -->
<li>Cross</li>
<li>Cross</li>
<li>Cross</li>
<li>Cross</li>
<li>Cross</li>
<li>Cross</li>
<li>Cross</li>
<li>Cross</li>
<li>Cross</li>
<li>Cross</li>
<li>Cross</li>
<li>Cross</li>
<li>Cross</li>
</ul>
<div class="contact-gray-bar">K</div>
<ul class="contact">
<!-- insert records here -->
<li>Kiwi</li>
</ul>
</br>
</div>
</div>
</div>
</div>
</div>
<强> JavaScript的:强>
$(function () {
$('.search-field').on('keyup', function () {
var first_result = false;
var text_s = $(this).val().toLowerCase();
//Reinit li elements and the contact-list position
$("li").removeClass("highlight");
$(".contact-list").scrollTop($(".contact-list").position().top);
if (text_s.length > 0) {
$("li").each(function () {
var li_value = $(this).text().toLowerCase();
if (li_value.indexOf(text_s) >= 0) {
$(this).addClass("highlight");
if (first_result == false) {
//Have to move to the 1rst element's position
first_result = true;
$(".contact-list").scrollTop($(this).position().top);
}
}
});
}
});
});
CSS:没有做任何更改。
如果您想了解有关职位的更多信息,请参阅:http://api.jquery.com/position/
答案 1 :(得分:0)
尝试类似的东西
$(function() {
$('.search-field').on('keyup blur change', function() {
var text_s = $(this).val();
$("li").removeClass("highlight");
if (text_s.length > 0){
$("li").each(function(){
var li_value = $(this).text();
//alert(this.id);
if (li_value.indexOf(text_s) >= 0){
//alert(text_s);
$(this).addClass("highlight");
var x = getOffset( document.getElementById(this.id) ).left;
}
});
}
});
});
function getOffset( el ) {
var _x = 0;
var _y = 0;
while( el && !isNaN( el.offsetLeft ) && !isNaN( el.offsetTop ) ) {
_x += el.offsetLeft - el.scrollLeft;
_y += el.offsetTop - el.scrollTop;
el = el.offsetParent;
}
window.scrollTo(_x,_y);
return { top: _y, left: _x };
}