jQuery.fn.selectHighlight = function(number) { return this.find("span.highlight:eq("+number+")").addClass('selectHighlight').end(); };
.highlight { background-color: gray; color: white } .selectHighlight { background-color: rgb(35, 140, 0) }
<div id="search_block"> <label> Search: <input id="search_text" type="text" value=""/> </label> <input id="search_button" type="button" value="Search"/> <input id="prev_search" type="button" value="<"/> <input id="next_search" type="button" value=">"/> <input id="clear_button" type="button" value="Clear"/> </div>
function scroll_to_word(){ pos = $('#text .selectHighlight').position() $('#content').jqxPanel('scrollTo', 0, pos.top - 5); }
var search_number = 0; // var search_count = 0; // //search - "search_button" $('#search_button').click(function() { $('#text').removeHighlight(); txt = $('#search_text').val(); if (txt == '') return; $('#text').highlight(txt); search_count = $('#text span.highlight').size() - 1; search_number = 0; $('#text').selectHighlight(search_number); // scroll_to_word(); // }); //clear - "clear_button" $('#clear_button').click(function() { $('#text').removeHighlight(); }); //prev_search - $('#prev_search').click(function() { if (search_number == 0) return; $('#text .selectHighlight').removeClass('selectHighlight'); search_number--; $('#text').selectHighlight(search_number); scroll_to_word(); }); //next_search - $('#next_search').click(function() { if (search_number == search_count) return; $('#text .selectHighlight').removeClass('selectHighlight'); search_number++; $('#text').selectHighlight(search_number); scroll_to_word(); });
function scroll_to_word(){...}
- scroll (rewind) function of the page to the word we need.highlight
and the number eq('+search_number +')
( search_number
- look in the list of global script variables). jQuery('#content').scrollTo('.highlight('+search_number +')');
$('#jqxPanel').jqxPanel('scrollTo', 10, 20);
function scroll_to_word(){ pos = $('#text .selectHighlight').position() $('#content').jqxPanel('scrollTo', 0, pos.top - 5); // 5 - - }
Source: https://habr.com/ru/post/180573/
All Articles