Skip to content

Mini Help System with jQuery

Tip 조회 수 5611 추천 수 0 2013.01.23 15:10:56

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 );
    }
});



profile

I see no changes, wake up in the morning and I ask myself

Is life worth living should I blast myself

Things would never be the same.

첨부
엮인글 :
http://adminplay.com/169206/292/trackback

l2zeo님은 글작성 및 댓글 포인트 ...

2013.01.23 15:10:56
*.52.33.196

l2zeo님은 50포인트에 당첨되셨습니다.
List of Articles
번호 제목 글쓴이 날짜 조회 수
47 Tip IE 버그와 해결책들 l2zeo 2013-07-03 8077
46 Tip IE css핵 (css hack)과 IE filter file [1] l2zeo 2013-07-03 14542
45 Tip 그누보드 패밀리사이트에서 회원가입연동 l2zeo 2013-05-31 5912
44 Tip robots.txt 관련 Tip. l2zeo 2013-05-30 9507
43 Tip jquery 관련 l2zeo 2013-05-26 6351
42 Tip Cascading Style Sheets (자바스크립트 style 객체) l2zeo 2013-04-25 6265
41 Tip 스토리보드 이제 쉬워졌어요~~ PowerMockUp 소개 해 봅니다.^^ file l2zeo 2013-03-18 6774
40 Tip 웹 서비스 구현시 캐시 구현을 남발해서는 안되는 이유? l2zeo 2013-03-18 6570
39 Tip 포인트가 마이너스일때 = 포인트 마이너스 회원들 포인트를 0으로 ... l2zeo 2013-03-15 9488
38 Tip 일정 포인트 이상은 적립안되게 하기 l2zeo 2013-03-15 5072
37 Tip CSS shorthand properties (단축 속성) [1] l2zeo 2013-02-26 5360
36 Tip 마우스 오버할때 아이디 안보이게 하기 file l2zeo 2013-02-03 5766
35 Tip 모바일에서도 이미지 리사이징 기능 적용하기 l2zeo 2013-01-24 51000
34 Tip 모바일에서 동영상 크기 지정해서 보여줄때. l2zeo 2013-01-24 6894
33 Tip 썸네일 생성이 되지 않을때 확인할것 file l2zeo 2013-01-24 7440
» Tip Mini Help System with jQuery file [1] l2zeo 2013-01-23 5611
31 Tip CSS3 를 이용한 툴팁(Tooltip) 표시 [1] l2zeo 2013-01-23 6054
30 Tip [caching tip] js, css 캐싱 팁 l2zeo 2013-01-23 5333
29 Tip 페이스북 담벼락에 자동 게시하는 법 l2zeo 2013-01-23 9614
28 Tip [Lazy Load Plugin for jQuery] 효율적인 이미지 로딩 file l2zeo 2013-01-23 10217

Copyright ADMINPLAY corp. All rights reserved.

abcXYZ, 세종대왕,1234

abcXYZ, 세종대왕,1234