How to show button on div mouse hover

前端 未结 5 1463
暗喜
暗喜 2020-12-11 04:42

i want to show button on div hover. when i hover mouse on div then button show otherwise hide.

my button in divbutton div.

html

         


        
相关标签:
5条回答
  • 2020-12-11 05:23

    Use following jQuery to perform your task.

    Here is a jsfiddle demo

    $(document).ready(function () {
        $(document).on('mouseenter', '.divbutton', function () {
            $(this).find(":button").show();
        }).on('mouseleave', '.divbutton', function () {
            $(this).find(":button").hide();
        });
    });
    
    0 讨论(0)
  • 2020-12-11 05:28

    Mr. Alien's answer gives a nice CSS implementation. If you need in jquery, use this -

    $( ".divbutton" )
     .on("mouseenter", function() {
      $("button").show();
    })
    .on("mouseleave", function() {
      $("button").hide();
    });
    

    In pure JavaScript -

    var buttonDiv = document.getElementsByClassName("divbutton")[0];  //better use some id and then use getElementById
    
    buttonDiv.onmouseover = function() {
        document.getElementById("YourButtonId").style.display = 'block';
    }
    
    buttonDiv.onmouseout = function() {
        document.getElementById("YourButtonId").style.display = 'none';
    }
    
    0 讨论(0)
  • 2020-12-11 05:42

    first hide the button with transform property.

    button{
    transform:translate(100%,100%)
    //this will move the button right and buttom
    }
    

    then when you hover on div, you bring it back

    .divbutton:hover button{
         //class name should have been divButton
         transform:translate(0,0)}
    
    0 讨论(0)
  • 2020-12-11 05:45

    Try this:

    $('.divbutton').mouseover(function(event)
    {
       $(this).find('button').show();
    });
    
    $('.divbutton').mouseout(function(event)
    {
       $(this).find('button').hide();
    });
    
    0 讨论(0)
  • 2020-12-11 05:47

    Use the below selector

    button {
      display: none; /* Hide button */
    }
    
    .divbutton:hover button {
       display: block; /* On :hover of div show button */
    }
    

    Demo

    Also make sure you assign some height or min-height to your div element, else it will be 0 as it doesn't hold any content. Also, don't use display: none; as inline style, as inline styles have highest specificity, and hence, you will be forced to use !important which is bad.

    In the above example am using button {/*Styles*/} but that is a general element selector, so make sure you define a class to your button element.

    0 讨论(0)
提交回复
热议问题