How to make a div appear by following the cursor?

Asked

Viewed 826 times

2

Good afternoon, you guys. My question is the following, I am creating a payment form, I have the following code:

$(".spanhover").hover(function(event) {
    var divid = "#popup1"
    $(divid).css({top: event.clientY, left: event.clientX}).show();
}, function() {
    var divid = "#popup1"
    $(divid).hide();
});
.credit-card-box .panel-title {
	    display: inline;
	    font-weight: bold;
	}
	.credit-card-box .form-control.error {
	    border-color: red;
	    outline: 0;
	    box-shadow: inset 0 1px 1px rgba(0,0,0,0.075),0 0 8px rgba(255,0,0,0.6);
	}
	.credit-card-box label.error {
	  font-weight: bold;
	  color: red;
	  padding: 2px 8px;
	  margin-top: 2px;
	}
	.credit-card-box .payment-errors {
	  font-weight: bold;
	  color: red;
	  padding: 2px 8px;
	  margin-top: 2px;
	}
	.credit-card-box label {
	    display: block;
	}
	
	.credit-card-box .display-table {
	    display: table;
	}
	.credit-card-box .display-tr {
	    display: table-row;
	}
	.credit-card-box .display-td {
	    display: table-cell;
	    vertical-align: middle;
	    width: 50%;
	}
	
	.credit-card-box .panel-heading img {
	    min-width: 180px;
	}
  
  .spanhover {
    cursor: pointer;
  }
  .popup {
    position: absolute;
    display: none;
    width: 50px;
    height: 50px;
    border: 1px solid #000;
    z-index: 999;
    background-color: #fff;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<html>
<head>
<link href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
</head>
<body>
   
  <div class="panel panel-default credit-card-box">
			                <div class="panel-heading" style="min-height: 40px;">
			                    <div class="col-md-7" >
			                        <h3 class="panel-title display-td">Meio de pagamento</h3>
			                    </div>
			                    <div class="col-md-5">
			                    	<i class="fa fa-2x fa-cc-visa"></i><i class="fa fa-2x fa-cc-mastercard"></i>
			                    </div>                    
			                </div>
			                <div class="panel-body">
			                	<div class="row text-center">
			                		<h4>Dados do proprietário do cartão*</h4>
			                	</div>
			                	<div class="row">
		                            <div class="col-xs-12">
		                                <div class="form-group">
		                                    <label for="cardNumber" style="	top:-15px !important;
																			font-size:14px !important;
																			color:#5264AE !important;">Nome completo</label>
		                                    <div class="form-group">
		                                        <input 
		                                            type="text"
		                                            class="form-control"
		                                            name="cardName"
		                                            placeholder="Nome (conforme escrito no cartão)"
		                                            autocomplete="cc-name"
		                                            required autofocus 
		                                        />
		                                    </div>
		                                </div>                            
		                            </div>
		                        </div>
			                	<div class="row">
			                		<div class="col-xs-8">
		                                <div class="form-group">
		                                    <label for="cardNumber" style="	top:-15px !important;
																			font-size:14px !important;
																			color:#5264AE !important;">cpf</label>
		                                    <div class="form-group">
		                                        <input 
		                                            type="tel"
		                                            class="form-control"
		                                            name="cpf"
		                                            placeholder="CPF"
		                                            autocomplete="cpf"
		                                            required 
		                                        />
		                                    </div>
		                                </div>                            
		                            </div>
		                            <div class="col-xs-4">
		                                <div class="form-group">
		                                    <label for="cardNumber" style="	top:-15px !important;
																			font-size:14px !important;
																			color:#5264AE !important;">Nascimento</label>
		                                    <div class="form-group">
		                                        <input 
		                                            type="tel"
		                                            class="form-control"
		                                            name="birthDate"
		                                            placeholder="dd/mm/aaaa"
		                                            autocomplete="dt-nasc"
		                                            required
		                                        />
		                                    </div>
		                                </div>                            
		                            </div>
			                	</div>
		                        <div class="row">
		                            <div class="col-xs-12">
		                                <div class="form-group">
		                                    <label for="cardNumber" style="	top:-15px !important;
																			font-size:14px !important;
																			color:#5264AE !important;">Número do cartão</label>
		                                    <div class="input-group">
		                                        <input 
		                                            type="tel"
		                                            class="form-control"
		                                            name="cardNumber"
		                                            placeholder="Número do cartão"
		                                            autocomplete="cc-number"
		                                            required
		                                        />
		                                        <span class="input-group-addon"><i class="fa fa-credit-card"></i></span>
		                                    </div>
		                                </div>                            
		                            </div>
		                        </div>
		                        <div class="row">
		                            <div class="col-xs-7 col-md-7">
		                                <div class="form-group">
		                                    <label for="cardExpiry" style="	top:-15px !important;
																			font-size:14px !important;
																			color:#5264AE !important;"><span class="hidden-xs">Expira em</span><span class="visible-xs-inline">Data</span></label>
			                                    <input 
			                                        type="tel" 
			                                        class="form-control" 
			                                        name="cardExpiry"
			                                        placeholder="MM / AA"
			                                        autocomplete="cc-exp"
			                                        required 
			                                    />
		                                </div>
		                            </div>
                                <div class="popup" id="popup1" style="display:none;">test1</div>
		                            <div class="col-xs-5 col-md-5 pull-right">
		                                <div class="form-group">
		                                    <label for="cardCVC" class="spanhover" id="hover1" style="	top:-15px !important;
																			font-size:14px !important;
																			color:#5264AE !important;">CVC</label>
			                                    <input 
			                                        type="tel" 
			                                        class="form-control"
			                                        name="cardCVC"
			                                        placeholder="CVC"
			                                        autocomplete="cc-csc"
			                                        required
			                                    />
		                                </div>
		                            </div>
		                        </div>
		                        <div class="row">
		                        	<div class="col-xs-12">
		                        		<p class="small btn-danger text-center">* estes dados não são armazenados.</p>
		                        	</div>
		                        </div>
		                        <div class="row" style="display:none;">
		                            <div class="col-xs-12">
		                                <p class="payment-errors"></p>
		                            </div>
		                        </div>
			                    <div class="row">
			                    	<div class="col-xs-12">
			                    		<input type="submit" class="btn btn-success btn-block" value="Finalizar">
			                    	</div>
			                    </div>
			                </div>
			            </div>
                  </body>
                  </html>

The idea was to show an image of how to find the CVC on the card, so that would put it in a div that would appear when passing the mouse over the CVC field, the result is that the div accompanies the cursor while it is on top of the label, but I got stuck and from here I can not pass... someone can give me a light?

  • I particularly do not see the need for the div to accompany the mouse by the label, until the label is small, only has 3 letters.

  • I intend to use this in other points of the system

2 answers

5


This would be a possible solution, just replace it with whatever you need:

 $( "#holder" ).on( "mousemove", function( event ) {
        $("#popover").show().css({left:event.pageX, top:event.pageY } )
 })
 $( "#holder" ).on('mouseleave',function(){
 $('#popover').hide()
 })
#holder{
  width: 150px;
  height: 50px;
  background: #000;
}
#popover{
  cursor: default;
  display:none;
  height: 20px;
  background: #f00;
  position: absolute;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<br></br>
<div id="holder">
  <div id="popover">
    CVC
  </div>
</div>

When you hover over #Holder, add the mouse position as offset of #Popover and exit #Holder hides it.

  • It doesn’t work, young man. pageX and pageY take the absolute cursor position relative to the whole window. The treatments I quoted in my reply.

  • If you add some <br> before your div, you will see that Popover will appear far away from the cursor.

  • True, missed take the relative position of the div #Holder

  • Now improved!

2

Instead of using .hover, listening to two events, mouseenter and mouseleave, you would need to use two events, mousemove and mouseleave. The first will make the element follow the cursor as you move it and the second will hide the div.

To make sure the cursor is not over the div, add a difference to the values of the cursor position. In this case, I added 5px on the X axis and subtract the height of the div on the Y axis, but it is still necessary to compensate for the position by adding the value of the window scroll:

$(".spanhover").on("mousemove mouseleave", function(event) {
   
   var divid = "#popup1";
   if(event.type == "mouseleave"){
      $(divid).hide();
   }else{
      var divid_height = $(divid).height(); // altura da div
      var scrolTop = $(window).scrollTop(); // scroll da janela
      $(divid).css({
         top: event.clientY-divid_height+scrolTop,
         left: event.clientX+5
      }).show();
   }
   
});
.spanhover{
   cursor: pointer;
}
.credit-card-box .panel-title {
	    display: inline;
	    font-weight: bold;
	}
	.credit-card-box .form-control.error {
	    border-color: red;
	    outline: 0;
	    box-shadow: inset 0 1px 1px rgba(0,0,0,0.075),0 0 8px rgba(255,0,0,0.6);
	}
	.credit-card-box label.error {
	  font-weight: bold;
	  color: red;
	  padding: 2px 8px;
	  margin-top: 2px;
	}
	.credit-card-box .payment-errors {
	  font-weight: bold;
	  color: red;
	  padding: 2px 8px;
	  margin-top: 2px;
	}
	.credit-card-box label {
	    display: block;
	}
	
	.credit-card-box .display-table {
	    display: table;
	}
	.credit-card-box .display-tr {
	    display: table-row;
	}
	.credit-card-box .display-td {
	    display: table-cell;
	    vertical-align: middle;
	    width: 50%;
	}
	
	.credit-card-box .panel-heading img {
	    min-width: 180px;
	}
  
  .spanhover {
    cursor: pointer;
  }
  .popup {
    position: absolute;
    display: none;
    width: 50px;
    height: 50px;
    border: 1px solid #000;
    z-index: 999;
    background-color: #fff;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<html>
<head>
<link href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
</head>
<body>
   
  <div class="panel panel-default credit-card-box">
			                <div class="panel-heading" style="min-height: 40px;">
			                    <div class="col-md-7" >
			                        <h3 class="panel-title display-td">Meio de pagamento</h3>
			                    </div>
			                    <div class="col-md-5">
			                    	<i class="fa fa-2x fa-cc-visa"></i><i class="fa fa-2x fa-cc-mastercard"></i>
			                    </div>                    
			                </div>
			                <div class="panel-body">
			                	<div class="row text-center">
			                		<h4>Dados do proprietário do cartão*</h4>
			                	</div>
			                	<div class="row">
		                            <div class="col-xs-12">
		                                <div class="form-group">
		                                    <label for="cardNumber" style="	top:-15px !important;
																			font-size:14px !important;
																			color:#5264AE !important;">Nome completo</label>
		                                    <div class="form-group">
		                                        <input 
		                                            type="text"
		                                            class="form-control"
		                                            name="cardName"
		                                            placeholder="Nome (conforme escrito no cartão)"
		                                            autocomplete="cc-name"
		                                            required autofocus 
		                                        />
		                                    </div>
		                                </div>                            
		                            </div>
		                        </div>
			                	<div class="row">
			                		<div class="col-xs-8">
		                                <div class="form-group">
		                                    <label for="cardNumber" style="	top:-15px !important;
																			font-size:14px !important;
																			color:#5264AE !important;">cpf</label>
		                                    <div class="form-group">
		                                        <input 
		                                            type="tel"
		                                            class="form-control"
		                                            name="cpf"
		                                            placeholder="CPF"
		                                            autocomplete="cpf"
		                                            required 
		                                        />
		                                    </div>
		                                </div>                            
		                            </div>
		                            <div class="col-xs-4">
		                                <div class="form-group">
		                                    <label for="cardNumber" style="	top:-15px !important;
																			font-size:14px !important;
																			color:#5264AE !important;">Nascimento</label>
		                                    <div class="form-group">
		                                        <input 
		                                            type="tel"
		                                            class="form-control"
		                                            name="birthDate"
		                                            placeholder="dd/mm/aaaa"
		                                            autocomplete="dt-nasc"
		                                            required
		                                        />
		                                    </div>
		                                </div>                            
		                            </div>
			                	</div>
		                        <div class="row">
		                            <div class="col-xs-12">
		                                <div class="form-group">
		                                    <label for="cardNumber" style="	top:-15px !important;
																			font-size:14px !important;
																			color:#5264AE !important;">Número do cartão</label>
		                                    <div class="input-group">
		                                        <input 
		                                            type="tel"
		                                            class="form-control"
		                                            name="cardNumber"
		                                            placeholder="Número do cartão"
		                                            autocomplete="cc-number"
		                                            required
		                                        />
		                                        <span class="input-group-addon"><i class="fa fa-credit-card"></i></span>
		                                    </div>
		                                </div>                            
		                            </div>
		                        </div>
		                        <div class="row">
		                            <div class="col-xs-7 col-md-7">
		                                <div class="form-group">
		                                    <label for="cardExpiry" style="	top:-15px !important;
																			font-size:14px !important;
																			color:#5264AE !important;"><span class="hidden-xs">Expira em</span><span class="visible-xs-inline">Data</span></label>
			                                    <input 
			                                        type="tel" 
			                                        class="form-control" 
			                                        name="cardExpiry"
			                                        placeholder="MM / AA"
			                                        autocomplete="cc-exp"
			                                        required 
			                                    />
		                                </div>
		                            </div>
                                <div class="popup" id="popup1" style="display:none;">test1</div>
		                            <div class="col-xs-5 col-md-5 pull-right">
		                                <div class="form-group">
		                                    <label for="cardCVC"  id="hover1" class="spanhover" style="	top:-15px !important;
																			font-size:14px !important;
																			color:#5264AE !important;">CVC</label>
			                                    <input 
			                                        type="tel" 
			                                        class="form-control"
			                                        name="cardCVC"
			                                        placeholder="CVC"
			                                        autocomplete="cc-csc"
			                                        required
			                                    />
		                                </div>
		                            </div>
		                        </div>
		                        <div class="row">
		                        	<div class="col-xs-12">
		                        		<p class="small btn-danger text-center">* estes dados não são armazenados.</p>
		                        	</div>
		                        </div>
		                        <div class="row" style="display:none;">
		                            <div class="col-xs-12">
		                                <p class="payment-errors"></p>
		                            </div>
		                        </div>
			                    <div class="row">
			                    	<div class="col-xs-12">
			                    		<input type="submit" class="btn btn-success btn-block" value="Finalizar">
			                    	</div>
			                    </div>
			                </div>
			            </div>
                  </body>
                  </html>

  • 1

    Excellent, it was exactly what I needed!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.