Skip to content

Tip
2013.01.23 15:10

Mini Help System with jQuery

조회 수 6756 추천 수 0 댓글 1
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄 첨부
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄 첨부

a1.jpg

index.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>Mini Help System with jQuery | Tutorialzine </title>

        <!-- Our stylesheet -->
        <link rel="stylesheet" href="assets/css/styles.css" />

    </head>
    <body>

        <div id="widget">

            <div id="header">
                <input type="text" id="search" placeholder="Search in the text" />
            </div>

            <div id="content">
                <!-- Your help text goes here -->
            </div>
        </div>​​

        <!-- JavaScript Includes -->
        <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
        <script src="assets/js/highlight.jquery.js"></script>
        <script src="assets/js/jquery.scrollTo.min.js"></script>
        <script src="assets/js/script.js"></script>
    </body>
</html>


assets/js/highlight.jquery.js

(function($) {

    var termPattern;

    $.fn.highlight = function(term, callback) {

        return this.each(function() {

            var elem = $(this);

            if (!elem.data('highlight-original')) {

                // Save the original element content
                elem.data('highlight-original', elem.html());

            } else {

                // restore the original content
                elem.highlightRestore();

            }

            termPattern = new RegExp('(' + term + ')', 'ig');

            // Search the element's contents
            walk(elem);

            // Trigger the callback
            callback && callback(elem.find('.match'));

        });
    };

    $.fn.highlightRestore = function() {

        return this.each(function() {
            var elem = $(this);
            elem.html(elem.data('highlight-original'));
        });

    };

    function walk(elem) {

        elem.contents().each(function() {

            if (this.nodeType == 3) { // text node

                if (termPattern.test(this.nodeValue)) {
                    // wrap the match in a span:
                    $(this).replaceWith(this.nodeValue.replace(termPattern, '<span class="match">$1</span>'));
                }
            } else {
                // recursively call the function on this element
                walk($(this));
            }
        });
    }

})(jQuery);



assets/js/script.js

$(function() {

    var search = $('#search'),
        content = $('#content'),
        matches = $(), index = 0;

    // Listen for the text input event
    search.on('input', function(e) {

        // Only search for strings 2 characters or more
        if (search.val().length >= 2) {

            // Use the highlight plugin
            content.highlight(search.val(), function(found) {

                matches = found;

                if(matches.length && content.is(':not(:animated)')){
                    scroll(0);
                }

            });
        } else {
            content.highlightRestore();
        }

    });

    search.on('keypress', function(e) {

        if(e.keyCode == 13){ // The enter key
            scrollNext();
        }

    });

    function scroll(i){
        index = i;

        // Trigger the scrollTo plugin. Limit it
        // to the y axis (vertical scroll only)
        content.scrollTo(matches.eq(i), 800, { axis:'y' } );
    }

    function scrollNext(){
        matches.length && scroll( (index + 1) % matches.length );
    }
});



  • ?
    l2zeo님은 글작성 및 댓글 포인트 ... 2013.01.23 15:10
    l2zeo님은 50포인트에 당첨되셨습니다.

List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
47 Tip IE 버그와 해결책들 l2zeo 2013.07.03 9157
46 Tip IE css핵 (css hack)과 IE filter 1 file l2zeo 2013.07.03 15697
45 Tip 그누보드 패밀리사이트에서 회원가입연동 l2zeo 2013.05.31 7097
44 Tip robots.txt 관련 Tip. l2zeo 2013.05.30 12142
43 Tip jquery 관련 l2zeo 2013.05.26 7517
42 Tip Cascading Style Sheets (자바스크립트 style 객체) l2zeo 2013.04.25 7536
41 Tip 스토리보드 이제 쉬워졌어요~~ PowerMockUp 소개 해 봅니다.^^ file l2zeo 2013.03.18 8313
40 Tip 웹 서비스 구현시 캐시 구현을 남발해서는 안되는 이유? l2zeo 2013.03.18 7675
39 Tip 포인트가 마이너스일때 = 포인트 마이너스 회원들 포인트를 0으로 ... l2zeo 2013.03.15 11799
38 Tip 일정 포인트 이상은 적립안되게 하기 l2zeo 2013.03.15 6259
37 Tip CSS shorthand properties (단축 속성) 1 l2zeo 2013.02.26 6581
36 Tip 마우스 오버할때 아이디 안보이게 하기 file l2zeo 2013.02.03 6843
35 Tip 모바일에서도 이미지 리사이징 기능 적용하기 l2zeo 2013.01.24 58263
34 Tip 모바일에서 동영상 크기 지정해서 보여줄때. l2zeo 2013.01.24 8104
33 Tip 썸네일 생성이 되지 않을때 확인할것 file l2zeo 2013.01.24 8909
» Tip Mini Help System with jQuery 1 file l2zeo 2013.01.23 6756
31 Tip CSS3 를 이용한 툴팁(Tooltip) 표시 1 l2zeo 2013.01.23 7247
30 Tip [caching tip] js, css 캐싱 팁 l2zeo 2013.01.23 6895
29 Tip 페이스북 담벼락에 자동 게시하는 법 l2zeo 2013.01.23 10811
28 Tip [Lazy Load Plugin for jQuery] 효율적인 이미지 로딩 file l2zeo 2013.01.23 11404
Board Pagination Prev 1 2 3 Next
/ 3

Copyright ADMINPLAY corp. All rights reserved.

abcXYZ, 세종대왕,1234

abcXYZ, 세종대왕,1234