Fade-out on mouseout, not on hover - hover

I have a menu where I am trying to make the sub-menu fade in on mouseover, and fade out on mouseleave. I have tried several solutions, most of them resulting in the menu fading out immediately on hover, and not on mouseleave/mouseout.
The code below is the one I believe makes the most sense. But the result is that the menu fades in, but doesn't fade out.
<script type="text/javascript">
$(document).ready(function(){
//When hovering a top-level link, submenu fadein.
$('.toppunkt a').mouseenter(function(){
$('ul.sub-menu').fadeIn();
});
//When leaving the submenu, fadeout.
$('.ul.sub-menu').mouseleave(function(){
$('ul.sub-menu').fadeOut();
});
});
</script>

This may or may not help you but you seem to be checking the wrong item on mouseleave...
http://jsfiddle.net/Mutmatt/3ppr8/14/
Even better, the way that you PROBABLY want this menu system to behave is like this jsfiddle:
http://jsfiddle.net/Mutmatt/3ppr8/23/
Take a look at that one. Don't forget to mark correct answers for future reference
Code:
JS:
jQuery(document).ready(function() {
jQuery('#topmenu li').hover(
//When hovering a top-level link, submenu fadein.
function() {
jQuery('ul', this).stop().fadeIn();
},
//When leaving the submenu, fadeout.
function() {
jQuery('ul', this).stop().fadeOut();
}
);
});​
HTML:
<ul id="topmenu">
<li>yep
<ul class="sub-menu" style="display: none;">
<li>derp</li>
<li>yerp</li>
</ul>
</li>
</ul>​

Could be the extra '.' in getting the sub-menu in the mouse leave function.
I wrote up a solution using divs.
Here is the fiddle:
http://jsfiddle.net/PAWQr/12/
Hopefully this helps.
HTML:
<div class="toppunkt">
Here is a list
<div class="sub-menu" style="width:70px; border: 1px dotted gray; display: none;">
<ul>
<li>Option1</i>
<li>Option2</i>
</ul>
</div>
</div>
Script:
$(document).ready(function(){
//When hovering a top-level link, submenu fadein.
$('.toppunkt a').mouseenter(function(){
//alert('mouse enter');
$('.sub-menu').fadeIn();
});
//When leaving the submenu, fadeout.
$('.sub-menu').mouseleave(function(){
$('.sub-menu').fadeOut();
});
​});

Related

focusout() not working as expected (in this very basic example)

I'm trying to create a custom dropdown with two simple elements -- a div for the dropdown header, and a div to contain the items. When the header-div is clicked, the items-div is to be opened, and when the items-div loses focus, it is to be closed.
Code (HTML):
<div id="dd_header" style="width:200px;height:20px;border:1px solid gray"></div>
<div id="dd_items" style="width:200px;height:200px;border:1px solid gray">
Item 1<br/>
Item 2<br/>
Item 3<br/>
</div>
Code (JS):
$("#dd_items").hide();
$("#dd_header").click(function () {
$("#dd_items").show();
});
$("#dd_items").focusout(function () {
$("#dd_items").hide();
});
jsFiddle -- http://jsfiddle.net/FLnHG/
For some reason, the focusout event isn't firing. What am I missing?
(Note: Adding the focusout on #dd_header works, but that doesn't help because the user won't be able to make a selection from the items).
add js code in ready and use mouseout
$(document).ready(function(){
$("#dd_items").hide();
$("#dd_header").click(function () {
$("#dd_items").show();
});
$("#dd_items").mouseout(function () {
$("#dd_items").hide();
});
});
You can try this way:
$("#dd_items").hide();
$("#dd_header, #dd_items").click(function (e) {
e.stopPropagation(); ///<----this stops the event to bubble up at document
$("#dd_items").show();
});
$(document).click(function () {
$("#dd_items").hide();
});
Updated Fiddle
focus event is applicable to a limited set of elements, such as form elements (, , etc.) and links ().
Change:
<div id="dd_header"></div>
to
<input type ="text" id="dd_header" />
JS:
$("#dd_header").focusout(function () {
$("#dd_items").hide();
});
DEMO here.
Item 1<br/>
Item 2<br/>
Item 3<br/>
This are text and text can not get focus so they can not fire focusout event, You need to have link or control instead of this.check this fiddle
Try this:
$(document).ready(function(){
$("#dd_items").hide();
$("#dd_header").click(function () {
$("#dd_items").show();
});
$("#dd_items").mouseleave(function () {
$("#dd_items").hide();
});
});
HTML:
<div id="dd_header" tabindex="-1"></div>
<div id="dd_items">
Item 1<br/>
Item 2<br/>
Item 3<br/>
</div>
jQuery:
$("#dd_items").attr('tabindex',-1).focus(function () {
});
$(window).focusout(function () {
$("#dd_items").hide();
});
Well I'm not sure what you exactly want from the question but looking at the comments on other answers, I came up with a modification to your own fiddle.
I chose to do it without the use of jQuery. It's a very simple CSS solution to perhaps what you want. Only difference if any, is that the list will toggle on hover(instead of click as in your plan)
Try the fiddle and let us know if it works for you.
HTML
<div id="dd_header">
<div id="dd_items">
<ul style="list-style-type:none;">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>
</div>
CSS
#dd_header {
height: 20px;
width: 200px;
border: 1px solid lightgray;
}
#dd_items {
display: none;
position: absolute;
height:200px;
width: 200px;
background: #fbfbfb;}
#dd_header:hover #dd_items {display: block;}

change the background colour of divs when clicked

I have tabs comprised of the following, When I clicked on any of the divs, I need background colour change to blue and if I clicke on other div, the previous tabs colour needs to set to original and new clicked div colour to be blue and so on:
This is my html:
<div class="zoom_controls">
<a class="db" id="prof_cpu_d" href="#" data-chart="line" data-range="1m">Real Time</a>
<a class="db" id="prof_cpu_w"href="#" data-chart="line" data-range="3m">Weekly</a>
<a class="db" id="prof_cpu_m" href="#" data-chart="line" data-range="6m">Monthly</a>
</div>
I have this:
.zoom_controls a:active {
background-color: #a6d1ff;
}
it does not seem to be working
how would I do this in css?
You need to use Javascript/jQuery to toggle the class. You can't do this with pure CSS.
Modify CSS
.zoom_controls a.active {
background-color: #a6d1ff;
}
jQuery
$('.zoom_controls a').on('click', function(event){
event.preventDefault();
$('.zoom_controls a').removeClass('active'); //Remove color for all with class .zoom_controls
$(this).toggleClass('active'); //Apply bgcolor to clicked element
});
Codepen sketch
Update
If you are, for some reason, thinking about a:hover, then you do it like this in CSS.
.zoom_controls a:hover {
background-color: #a6d1ff;
}
Otherwise, if you are looking to target the 'active' state of the link, you are doing it correctly.
The :active state only applies when the anchor is active -- that is, when the user is clicking on it. When that click ends, so does the state. What you want can't be done with pure CSS.
CSS:
.zoom_controls a.active {
background-color: #a6d1ff;
}
jQuery:
$('.zoom_controls a').on('click', function(ev) {
ev.preventDefault();
$(this).addClass('active').siblings('.active').removeClass('active');
});
JSBin demo
You can use the JS library jquery to select the active tabs and assign the the background color. The nice bit with using jquery is it's compatibility with other browsers.
It does work, but it's not doing what you want. Because active is only a dynamic pseudo-class that determines the styling of an active element (in this case any link inside the div with class zoom_controls when it's being clicked).
You might need JavaScript for this job, that or an unnecessarily complex CSS 3 solution.

CSS only collapsing menu only works in firefox, not webkit browsers, why?

Got help earlier on how to get this collapsing menu to work. But now all of a sudden the links in it won't work. they just contract the the menu again. Any ideas?
Thanks a lot!
CSS:
#list, .show {display: none; }
.hide:focus + .show {display: inline; }
.hide:focus { display: none; }
.hide:focus ~ #list { display:block; }
#media print { .hide, .show { display: none; } }
li.folding {list-style-type:none; margin-left:-20px;}
HTML:
<div>
[Link]
[Link]
<ol id="list">
<li class="folding">Item 1</li>
<li class="folding">Item 2</li>
<li class="folding">Item 3</li>
</ol>
</div>
Your code works fine as it is..
Working JSFiddle. ( IN FIREFOX ONLY ! )
The problem is more browser compatibility, run the JSFiddle in firefox and it will work as you expect, on chrome and safari it does not work.
The better solution for this (for all browsers), is to use Javascript/jQuery to hide and show the menu on click of a button, this will work across all mobiles, tables & browsers consistently with minimal code.
Update
Here is a quick example of getting this working using jQuery. You can see the code is very small, easy to read and extend, and will also work on ALL browsers!
VIEW THE JSFIDDLE
HTML:
<div>
[Link]
<ol id="list">
<li class="folding">Item 1</li>
<li class="folding">Item 2</li>
<li class="folding">Item 3</li>
</ol>
</div>
Javascript/jQuery:
// When the page is loaded and ready
$(function(){
// On click of `.toggler` (the <a> with class `.toggler`)
$('.toggler').click(function(){
// Toggle the list menu (toggle means if hidden show it, else hide it)
$('#list').fadeToggle(); // also try.. `slideToggle()`
});
});
The above HTML & Javascript does exactly the same thing, except it will work on all devices!
View the JSFiddle - Javascript/jQuery Version
Since clicking another link makes the first one loose focus, the items in the list go back to hiding.
Since the focus loss is always done before executing the click behaviour, the click does not reach the link since it is not under your mouse anymore.

How to create a simple social sharing dropdown menu in CSS/HTML

How can I create a simple HTML/CSS social sharing menu similar to the one found at the bottom of each post on http://bitquill.com/ — (I know it's my site, but I didn't code the sharing menu. I'm using a Squarespace template and love their sharing menu and want to re-create it elsewhere.)
You have to use an API from each site to get the buttons/badges. For example, you have to review the docs for the Facebook like button: https://developers.facebook.com/docs/reference/plugins/like/ and get the code that way.
To create the menu:
Make a share button using a div, then put another div after it, which is the menu. Style to your liking. Then, make the menu display: none - this will hide it. Use JS to bind the button's click event to a function that shows the menu:
HTML
<div class="share">Share</div>
<div class="menu">
<ul>
<li>Facebook</li>
<li>Twitter</li>
<li>Stack</li>
</ul>
</div>
CSS
.menu {
display: none;
}
JS
$('body').on('click', function (e) {
if (e.target.className !== 'share')
$('.menu').css('display', 'none');
else
$('.menu').css('display', 'block');
});
So your entire HTML file should look like:
<html>
<head>
<style>
.menu {
display: none;
}
</style>
</head>
<body>
<div class="share">Share</div>
<div class="menu">
<ul>
<li>Facebook</li>
<li>Twitter</li>
<li>Stack</li>
</ul>
</div>
<!-- This is the jQuery -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$('body').on('click', function (e) {
if (e.target.className !== 'share')
$('.menu').css('display', 'none');
else
$('.menu').css('display', 'block');
});
</script>
</body>
</html>
Here is a quick example. You set up a container div (which must have position:relative), and menu div (which has positioned:absolute). Use jQuery to hide the menu div when the page loads. When a user clicks on Share, the div will be displayed.
The API code that you get from Facebook will be placed in the div that has the Facebook placeholder text.
To see more about how the menu in your example was implemented, open the page using Chrome. Right click on "Share" and go to "Inspect Element."
<script type="text/javascript">
$(document).ready(function () {
//Initally hide social-menu div
$("#social-menu").hide();
//When social-button is pressed, show social-menu
$("#social-button").click(function () {
$("#social-menu").show();
});
});
<div id="social-button" >Share</div>
<div id="social-container" style="position:relative;">
<div id="social-menu" style="position:absolute;top:0px;bottom:0px;z-index:10" >
<div>Facbook</div>
<div>Google +</div>
</div>
</div>

Achieve click event using just CSS

My question is: It is possible to achieve this example using only css? If not what would you do? Jsfiddle examples are really appreciated ;)
How to obtain also the slashes? Should i use an image or in css is possible? And the triangle that change when is clicked? I know it is possible to do it with Js maybe in css :after and :before would help me?
PS: Javascript to Hide Menu:
<script language="javascript">
function toggle() {
var ele = document.getElementById("toggleMenu");
var text = document.getElementById("displayMenu");
if(ele.style.display == "block") {
ele.style.display = "none";
text.innerHTML = "Menu";
}
else {
ele.style.display = "block";
text.innerHTML = "Hide";
}
}
</script>
<div class="menu-toggle"><div id="wrap"><a id="displayMenu" href="javascript:toggle();">Menu</a></div></div>
<div id="toggleMenu" style="display: none">
<div class="menu">
<ul><li> Home </li>
<li> Item </li>
</ul>
</div>
</div>
Usually I do something like this with images to achieve the click event with just css
<figure>
<img id="zoom" src="http://cargowire.net/Content/images/events/stackoverflow.jpg" alt="EE" />
<figcaption>
<ul>
<li>
Zoom In
</li>
<li>
Zoom Out
</li>
</ul>
</figcaption>
</figure>
and CSS:
figure { background: #e3e3e3; display: block; float: left;}
#zoom {
width: 0px;
-webkit-transition: width 1s;
}
#zoom:target {
width: 400px;
}
Check here: http://jsfiddle.net/dCTeW/ Maybe something similar can be done for menus too
It is perfectly possible, but only when the mouse hovers, not on click as far as I am aware. You will want to use CSS :hover states.
There is an in depth article here: http://csswizardry.com/2011/02/creating-a-pure-css-dropdown-menu/
and the demo for that article here: http://csswizardry.com/demos/css-dropdown/
If you want to use click then a small bit of jquery may help you something like:
$('.menu-item').click(function(){
$(this).find('hover-div').toggle()
})
http://api.jquery.com/toggle/
that is the Documentation for toggle which is what you need to achieve.
If you insist on using click in stead of hover, you could try to use :focus as suggested, but it would actually be a hack, and not considered correct use of HTML and css. Just for demonstration though, have a look at this: http://jsfiddle.net/9efMt/1/
As you can see, I use an input, with the :focus pseudo class and the + sibling selector. Nothing really wrong with that css, but putting the menu in an input is just not done!
I used jquery for the js in the, imo, correct example that is in the same fiddle. All i do is toggling a class when the menu link is clicked. It looks like this:
$('#menu2').click(function() {
$('#menu2-sub').toggleClass('active');
});
The css should be fairly straight forward.