0

Hi I'm using a jQuery custom underline plugin (below) the code is relatively simple, currently the underline appears with a mouseover function and disappears with mouseout, also only one instance of the underline can exist on he webpage at any one time. I would like to adapt the code so that the underline is permanent with regardless of mouse movement and several instances can exist on one page but am struggling to make it work. Here is the current code:

<script language="JavaScript" type="text/javascript">
   $(document).ready(function() {
        $('.underline a').underline({
            width:           4,                     //default: 1
            distance:        -1,                    //default: 0
            color:           '#66FFB2',     //default: #000
            durationOn:  350,                       //default: 250
            durationOff: 350,                       //default: 250
            extend:          2,                     //default: 2,
            linkOn:          'mouseover',   //default: 'mouseover'
            linkOff:         'mouseout'             //default: 'mouseout'
    });
    });
</script>



(function($){  
$.fn.underline = function(options) {
    var defaults = { 
                    width:       1,
                    distance:    0,
                    color:       '#000',
                    durationOn:  250,
                    durationOff: 250,
                    extend:      2,
                    linkOn:      'mouseover',
                    linkOff:     'mouseout'
                   };

    var options = $.extend(defaults, options);

    return this.each(function() { 
        obj = $(this);

        //Mouse Events
        var linkOn = options.linkOn;
        var linkOff = options.linkOff;

        obj.bind(linkOn, function() {
          $('#underlineLine').remove();

            var position  = $(this).offset();
            var top       = position.top;
            var left      = position.left;

            var objWidth  = $(this).width();
            var objHeight = $(this).height();

            $('body').append('<div id="underlineLine"></div>');

            $('#underlineLine').css({'position'        :'absolute',
                                     'display'             :'none',
                                     'height'           : options.width+'px',
                                      'background-color': options.color,});

            $('#underlineLine').css({'left' : left-options.extend,
                                     'top'  :      top+objHeight+options.distance,
                                     'width':     objWidth+options.extend*2 })
                                    .fadeIn(options.durationOn);

        });

        obj.bind(/*linkOff,*/ function() {
            $('#underlineLine').fadeOut(options.durationOff);

        });
    });  
};
})(jQuery);

<div class="underline"><a href="#">LINK</a></div>

Thanks!

  • `id="underlineLine"` to `class="underlineLine"` - edit CSS files as necessary, change all instances of `$('#underlineLine')` to `$('.underlineLine')`, then, remove the `obj.bind(linkOff` all together. then there will be no mouseout to remove the line, and you won't have duplicate ID problems, either. – Ohgodwhy Jul 18 '13 at 00:20
  • that leaves me with an underline that appears on mousehover and then remains permanent, how do I get them to load with the page? – Matt Woodman Jul 18 '13 at 07:55

0 Answers0