1

I am using the following code to show social icons on hover: You can see the site here: http://vitaminjdesign.com/adrian/

$('a').live('mouseover mouseout', function(event) {
          if (event.type == 'mouseover') {

            $('<a href="http://www.facebook.com/sharer.php?u=(this)" target="blank"><img src="images/facebook.gif" class="facebook" alt="facebook"></a>').appendTo(this).fadeIn(500);
            $('<a href="#"><img src="images/twitter.gif" class="twitter" alt="twitter"></a>').appendTo(this).fadeIn(500);
          } else {
            $('a').find('.facebook,.twitter').stop(true, true).fadeOut(500);
          }
        });

The problem I am having is within the facebook URL where you see (this). I want to dynamically add in the URL of the link associated with the current a being hovered. Basically, I want to add in the URL of the element being hovered on into the (this) area of the URL. Anyone?

1
  • Btw, it should be target="_blank" if you want each link to open in a new window and not just a new one that'll be named blank and each subsequent link you click will open it that window instead of a new one. Commented Jan 25, 2011 at 19:09

2 Answers 2

2

A little nicer way (in my opinion) to create elements is to use the properties object argument.

Also, I assume you only want to load the new elements once, then just fade them thereafter.

$('.rssRow').live('mouseenter mouseleave', function(event) {
      var twitFace = $(this).find('.facebook,.twitter');
      if (event.type == 'mouseover') {
            if( twitFace.length ) {
                twitFace.fadeIn(500);
            } else { 
                loadTwitFace.call(this);
            }
      } else {
            twitFace.stop(true, true).fadeOut(500);
      }
 });

function loadTwitFace() {

    $('<a>', { href:'http://www.facebook.com/sharer.php?u="' + $(this).find('a').attr('href') + '"',
             target:"blank"})
        .append($('<img>',{       src:'images/facebook.gif',
                            className:'facebook',
                                  alt:'facebook'}))
        .appendTo(this)
        .fadeIn(500);
}
Sign up to request clarification or add additional context in comments.

13 Comments

I tried this and it doesnt function as well.....It blinks and doesnt enable a solid hover
@JCHASE11: Somehow I didn't notice that you were doing it on hover. Do you really want to add a new duplicate <img> every time you hover? Seems like you should do it once, then just do a fadeIn/Out after that.
You are correct, that is probably not the best way of doing it, but I am not familiar with your method...If you can rewrite the code so it works with my hover, Id gladly accept
@JCHASE: I just updated. Now each <a> will only append new elements if they're not currently there. Give it a shot and let me know how it went.
@JCHASE11: Yeah...hovering over the links by The Vancouver Sun creates some pretty funky effects.
|
1
$('<a href="http://www.facebook.com/sharer.php?u=' 
    + $(this).attr("href") + ' target="blank">

1 Comment

you're missing a trailing double-quote after the href=""

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.