最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - div hover show buttonlink effect - Stack Overflow

programmeradmin1浏览0评论

here's the effect: when i move my mouse to the div, a delete link will show up. when i move out the mouse, the delete link disapper.

in the firebug, i can see that the delete link's style changes when i hover the div. but can someone tell me how to control this?

here's the effect: when i move my mouse to the div, a delete link will show up. when i move out the mouse, the delete link disapper.

in the firebug, i can see that the delete link's style changes when i hover the div. but can someone tell me how to control this?

Share Improve this question edited May 3, 2011 at 16:23 user147373 asked May 3, 2011 at 14:58 sbssbs 4,1526 gold badges43 silver badges56 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

Depending on how progressive you are (Support IE > 6), you could do it in CSS by creating a selector with the hover pseudo class of the parent. Using the same markup from your screenshots:

a.deleteFiddle {
  display: none;
}

div.lfiCont:hover a.deleteFiddle {
  display: inline;
}

See it in action - http://jsfiddle/7msZA/

If you want effects or have cross-browser concerns then JavaScript is the alternative. However, these days CSS is perfectly capable of handling this sort of UX.

As you see in your images, it's probably done with jQuery, a JavaScript library.

The code's quite simple:

HTML:

<div>
    Foo
    <span class="delete">Delete</span>
</div>

JavaScript:

$('div').hover(function() {
   $(this).find('.delete').show();
}, function() {
   $(this).find('.delete').hide();
});

CSS:

.delete {
    position: absolute;
    top: 0px;
    right: 0px;
    color: red;

    display: none;
}

div {
    padding: 50px;
    background-color: rgb(200, 200, 255);
    position: relative;
}

Demo: http://jsfiddle/euChd/3/

This is most likely done through Javascript, or jquery.

An example of how this is done could be something like this:

document.getElementById('div1').onmouseover = function() { 
    document.getElementById('div1').style.display = "inline";
}

You can do this with Javascript, I would suggest JQuery. This JQuery page has an example of changing styles based on hover.

The code example from that page is:

<!DOCTYPE html>
<html>
<head>
  <style>
  ul { margin-left:20px; color:blue; }
  li { cursor:default; }
  span { color:red; }
</style>
  <script src="http://code.jquery./jquery-git.js"></script>
</head>
<body>
  <ul>
    <li>Milk</li>
    <li>Bread</li>
    <li class='fade'>Chips</li>

    <li class='fade'>Socks</li>
  </ul>
<script>
$("li").hover(
  function () {
    $(this).append($("<span> ***</span>"));
  }, 
  function () {
    $(this).find("span:last").remove();
  }
);



//li with fade class
$("li.fade").hover(function(){$(this).fadeOut(100);$(this).fadeIn(500);});

</script>

</body>
</html>
发布评论

评论列表(0)

  1. 暂无评论