Display div on mouse hover - javascript

I have a link id="show" in header and a div id="display" in footer. I want to display div id="display" when mouse hover on id="show" that is in header area and div is in footer area.
HTML CODE:
<header>
<span> Show Div </span>
</header>
<---- Other Content -->
<footer>
<div id="display"> --- Content --- </div>
</footer>

Try this : You can use .hover() function as shown below. Pass comma seprated functions to it. First function to call when mouseover and second for mouseleave event.
$(function(){
$('#show').hover(function(){
$('#display').show();
},function(){
$('#display').hide();
});
}):

Without JQuery:
document.getElementById('show').onmouseover = function(evt) {
document.getElementById('display').style.display = "inline";
}

Hope this is what you are looking for.
http://jsfiddle.net/rxffwyux/
HTML
<header>
<span> Show Div </span>
</header>
<---- Other Content -->
<footer>
<div id="display"> --- Content --- </div>
</footer>
CSS
#display {
display: none;
}
Js
(function() {
$(function() {
//On Dom Ready
$('body').on({
mouseenter: function() {
$('#display').show();
},
mouseleave: function() {
$('#display').hide();
}
}, '#show');
});
}());

Related

on hover event hide the div

I want to hide the div 'sample' on hover event and need to show the div 'sample' on mouseout
$('.secmenu').hover(function() {
$('.sample').css('opacity', '0');
if ($('.secmenu').mouseleave()) {
$('.sample').css('opacity', '1');
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
click
<div class="sample">
hello div sample text content hello div sample text content
</div>
$('.secmenu').mouseenter(function() {//hide div on mouse enter
$('.sample').hide();
}).mouseleave(function() {//show div on mouse leave
$('.sample').show();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
click
<div class="sample">
hello div sample text content hello div sample text content
</div>
This can be done in css. No need of jQuery:
.secmenu:hover + .sample {
display: none;
}
click
<div class="sample">
hello div sample text content hello div sample text content
</div>
Hide div on hover and show div on mouseleave event. You need to bind the mouseleave event not write it in if condition.
$(document).ready(function () {
$('.secmenu').hover(function () {
$('.sample').css('opacity', '0');
});
$('.secmenu').mouseleave(function () {
$('.sample').css('opacity', '1');
});
});
Try this:
$('.secmenu').on('mouseenter',function() {
$('.sample').css('opacity', '0');
}).on('mouseleave',function(){
$('.sample').css('opacity', '1');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
click
<div class="sample">
hello div sample text content hello div sample text content
</div>
If .sample is immediate next sibling of .secmneu you don't need JavaScript or jQuery for this. You can do it with pure CSS.
.sample {
transition: opacity 0.25s ease;
opacity: 0;
}
.secmenu:hover + .sample {
opacity: 1;
}
click
<div class="sample">
hello div sample text content hello div sample text content
</div>
According to jQuery's documentation:
Bind one or two handlers to the matched elements, to be executed when the mouse pointer enters and leaves the elements.
.hover( handlerIn, handlerOut )
So you can do it like this (try it out):
$( '.secmenu' ).hover(
function() {
$('.sample').hide();
},
function() {
$('.sample').show();
}
);
Which is equivalent to:
$( selector ).mouseenter( handlerIn ).mouseleave( handlerOut );

JQuery FadeIn and FadeOut with button issue

I am trying to create a button to show all content. I want a fadein on mouse enter, fadeout on mouseleave. when you click the button it disables the fade out, until clicked again and that re-enables it
Here is the Jsfiddle: https://jsfiddle.net/uv4bxdxs/16/
<script>
function Show() {
$('#div1h1').fadeIn();
$('#div2h2').fadeIn();
$('#div3h3').fadeIn();
}
</script>
<body>
<button onclick="Show()">Show all</button>
<div id="div1">
<h1 id="div1h1">TEKST</h1>
</div>
<div id="div2">
<h1 id="div2h2">TEKST</h1>
</div>
<div id="div3">
<h1 id="div3h3">TEKST</h1>
</div>
</body>
One possible solution would be to add a class to displayed elements on the button click event. The class purpose is to disable the fade-in-out functionality. When the button is clicked again that class is removed to re-enable fade-in-out effect.
var flag = true;
$('button').click( function() {
if (flag) {
$('#div1h1').fadeIn().addClass('shown');
flag = false;
}
else {
$('#div1h1').fadeOut().removeClass('shown');
flag = true;
}
});
See DEMO
Please use this code:
$(function() {
var showAllFlag = false;
var btnShowAll = $('#show-all');
$('body').on('mouseenter', 'div.info-box', function() {
showTitle($(this))
}).on('mouseleave', 'div.info-box', function() {
hideTitle($(this))
});
function showTitle(target) {
target.find('h1').stop().fadeIn();
}
function hideTitle(target) {
if (!showAllFlag) {
target.find('h1').stop().fadeOut();
}
}
function showAllTitles() {
$('.info-box h1').show();
showAllFlag = true;
}
btnShowAll.on('click', showAllTitles);
});
Or follow by this link: enter link description here
Change your function to this:
$(function() {
$('#div1').hover(function() {
$('#div1h1').fadeIn();
});
});
$(function() {
$('#div2').hover(function() {
$('#div2h2').fadeIn();
});
});
$(function() {
$('#div3').hover(function() {
$('#div3h3').fadeIn();
});
});
Assuming that you just want the H1 to fade in and for them to stay visible on hover, just have the following code:
<script>
function Show() {
$('#div1h1').fadeIn();
$('#div2h2').fadeIn();
$('#div3h3').fadeIn();
}
</script>
<body>
<button onclick="Show()">Show all</button>
<div id="div1">
<h1 id="div1h1">TEKST</h1>
</div>
<div id="div2">
<h1 id="div2h2">TEKST</h1>
</div>
<div id="div3">
<h1 id="div3h3">TEKST</h1>
</div>
</body>
The JSFiddle link you provided has an extra JavaScript section which is causing the H1 to fade out on hover.
Just don't trigger the fadein if its already visible?
$('#div1').not(":visible").hover(function() {
$('#div1h1').fadeIn();
},
edit - My bad i didn't see that comma :), gimme a second

How to use Javascript or Jquery to toggle the visibility of a html div

I'm creating a basic site and I've got the following script that hides a div:
<script type="text/javascript">
<!--
function toggle_visibility(id) {
var e = document.getElementById(id);
if(e.style.display == 'block')
e.style.display = 'none';
else
e.style.display = 'block';
}
//-->
</script>
Show/Hide Description
<div id="description">
<br>
<br>
<p>This is a test paragraph</p>
</div>
When I load the page, the default is that the text is shown. How can I hide the text inside the div each time the page is loaded?
I want the user to manually click the button to view the text.
EDIT:
I've now added a picture of a + symbol, so that a user can click the + and the text appears by using the following lines:
<h4>Backstory</h4>
<img alt="" style="width: 20px; height: 20px;" src="./images/headerExpand.png" a href="#" onclick="toggle_visibility('backstory');">Show/Hide Description</a>
<div id="backstory" style="display: none">
<br>
<p>This is a new block of text</p>
</div>
The ./images/headerExpand.png is the icon of the + symbol
How would I change the + icon to a - icon once the + icon has been clicked?
Thanks.
set the display to none
<div id="description" style="display: none">
A redesigned solution might look like
<!-- Add a class toggler and the id of the target element as the href-->
Show/Hide Description
<!-- Add a class hidden so that the element is hidden when the page loads -->
<div id="description" class="hidden">
<br/>
<br/>
<p>This is a test paragraph</p>
</div>
CSS
.hidden {
display: none;
}
then jQuery script
// dom ready handler
jQuery(function () {
//register a click handelr for all anchor elements with class toggler
$('a.toggler').click(function (e) {
//prevent the default action of the event
e.preventDefault();
//togger the visibility of the element targetted by the href
$($(this).attr('href')).toggle()
});
})
Demo: Fiddle
use symply CSS :
<p style="display:none;">This is a test paragraph</p>
you have 2 options , set the css property right in the html like the other answers
<div id="description" style="display: none">
or wrap your javascript in something to tell the page to run the stript on page load
using jQuery
$(document).ready(function(){
//your code
});
or
$(function(){
//your code
});
or regular old javascript
window.onload = function(){
//your code
});

Toggle a div to be closed on load?

The following function toggles a div when clicked on the relative id. Any way to set it up so that when the page loads, the div being toggled starts out closed?
I dont want them to be seen until clicked on.
Best,
Joey
<script type="text/javascript">
$(document).ready(function() {
$("#architects").click(function() {
$(".exclusive-architects").toggle();
});
$("#international").click(function() {
$(".exclusive-international").toggle();
});
$("#designers").click(function() {
$(".exclusive-designers").toggle();
});
$("#historical").click(function() {
$(".exclusive-historical").toggle();
});
});
</script>
Just hide them on dom ready, like this:
<script type="text/javascript">
$(document).ready(function() {
$(".exclusive-architects, .exclusive-international,
.exclusive-designers, .exclusive-historical").hide();
$("#architects").click(function() {
$(".exclusive-architects").toggle();
});
$("#international").click(function() {
$(".exclusive-international").toggle();
});
$("#designers").click(function() {
$(".exclusive-designers").toggle();
});
$("#historical").click(function() {
$(".exclusive-historical").toggle();
});
});
</script>
You should just need to add a display:none to your starting CSS
DEMO
if your HTML looks like this:
<div id="buttons">
<div>toggle architects</div>
<div>toggle international</div>
<div>toggle designers</div>
<div>toggle historical</div>
</div>
<div class="cont"> architects content </div>
<div class="cont"> international content </div>
<div class="cont"> designers content </div>
<div class="cont"> historical content </div>
Than all you need is:
$('.cont').hide(); // HIDE ALL INITIALLY
$('#buttons div').click(function(){
$('.cont').eq( $(this).index() ).toggle();
});
$(function(){ // DOM ready
$(".exclusive-architects").hide();
$(".exclusive-international").hide();
$(".exclusive-designers").hide();
$(".exclusive-historical").hide();
});
it should work :)

slide toggle jquery - show a paragraph when clicking or hovering an image

Right,
I'm trying to use jQuery to show a paragraph when peoplel or hovering or clicking the image on top of the paragraph, Here is what I got so far:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"type="text/javascript"></script>
<script>
jQuery.noConflict;
jQuery(document).ready(function(){
//Hide the tooglebox when page load
jQuery(".toggle").hide();
//slide up and down when hover over img
jQuery("img").click(function(){
// slide toggle effect set to slow you can set it to fast too.
jQuery(this).next(".toggle").slideToggle("slow");
return true;
});
});
</script>
<p>Welcome!</p>
<h2>Choose your category</h2>
<div id="front-page">
<div id="front-page-row">
<div id="front-page-cell">
<p><img src="images/running.png" alt="Running" class="category"/></p>
<p class="toggle">List of events<br/>bldasfblda<br/>bdwfbwef</p>
</div>
<div id="front-page-cell">
<p><img src="images/mtb.png" alt="MountainBike" class="category"/></p>
<p class="toggle">List of events<br/>bldasfblda<br/>bdwfbwef</p>
</div>
<div id="front-page-cell">
<p><img src="images/music.png" alt="Music" class="category"/></p>
<p class="toggle">List of events<br/>bldasfblda<br/>bdwfbwef</p>
</div>
</div>
</div>
Nothing happens when I click the image
I would first hide the .toggle elements with CSS, so they're immediately made invisible. Secondly, I would avoid using #front-page-cell over and over as an ID, and instead convert it to a classname, and use it as a CSS selector.
<style>
.toggle { display: none }
</style>
<script>
jQuery.noConflict();
jQuery(function($){
$(".front-page-cell").on("click", "img.category", function(){
$(this).closest(".front-page-cell").find(".toggle").toggle("slow");
});
});
</script>
Demo: http://jsbin.com/evomay/edit#javascript,html
I think for your code part is suppose to be like below:
<script>
jQuery.noConflict;
jQuery(document).ready(function(){
//Hide the tooglebox when page load
jQuery(".toggle").hide();
//slide up and down when hover over img
jQuery("img").click(function(){
// slide toggle effect set to slow you can set it to fast too.
jQuery(this).parent().next(".toggle").slideToggle("slow");
return true;
});
});
</script>
Because next(selecetor) is looking for sibling. And .toggle is sibling of p that parent of img

Categories

Resources