Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Edit link on div mouseover

Tags:

javascript

On facebook for example - when you put your mouseover a news item, a remove button appears. How can I go about making this happen?

Thanks, Elliot

like image 989
Elliot Avatar asked May 17 '26 11:05

Elliot


2 Answers

Modern Browsers

In modern browsers, you can leverage the :hover pseudo class in our selector. As an example, consider the following markup:

<div class="item">
  <p>This is a long string of text</p>
  <div class="adminControls">
    <a href="#" title="Delete">Delete Item</a>
  </div>
</div>

By default, we would want the .adminControls to be hidden. They should, however, become visible once the user has hovered the .item element:

.item .adminControls {
    display: none;
}

.item:hover .adminControls {
    display: block;
}

JavaScript and jQuery

If you're using jQuery, you can accomplish this rather easily using the $.hover() method. If you're using Prototype, you can get the protoHover plugin to achieve the same result, or view this blog post.

$("div.item").hover(
  function () { $(this).find(".adminControls").show(); }, 
  function () { $(this).find(".adminControls").hide(); }
);

That would accomplish the show/hide effect for the following:

<div class="item">
  <p>This is a long string of text</p>
  <div class="adminControls">
    <a href="#" title="Delete">Delete Item</a>
  </div>
</div>

<div class="item">
  <p>This is a long string of text</p>
  <div class="adminControls">
    <a href="#" title="Delete">Delete Item</a>
  </div>
</div>

<div class="item">
  <p>This is a long string of text</p>
  <div class="adminControls">
    <a href="#" title="Delete">Delete Item</a>
  </div>
</div>
like image 107
Sampson Avatar answered May 20 '26 00:05

Sampson


If you don't need to support IE6, you can use the :hover pseudoclass like so:

CSS:

.link { display: none; }
.item:hover > .link { display: inline; }

HTML:

<div class="item">
  <a href="#" class="link">Remove</a>
  Lorem Ipsum...
</div>
like image 24
Annabelle Avatar answered May 20 '26 02:05

Annabelle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!