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;}
Related
Not sure if this is possible but I'm trying to display a div if another div which doesn't share the same parent is hovered.
The html looks something like this:
<div class="test">
<div class="hover-me"><p>Hover</p></div>
</div>
// some other content here
<div class="hover-content">
<p>hovered content</p>
</div>
I've tried using
.test:hover + .hover-content {
display: block;
}
But I think this only works if there's no other content in-between? Any suggestions?
Use javascript to listen to the onmouseover event, or jquery to handle the hover event on one and change the display attribute of the other. Using jquery
<script>
$(document).ready(function () {
$(".hover-me").hover(function () {
$(".hover-content").show();
}, function() {
$(".hover-content").hide();
});
});
</script>
If you don't want to use jquery, change your html like so
<div class="test">
<div class="hover-me"
onmouseover="document.getElementById('hover-content').style.display = 'block';"
onmouseout="document.getElementById('hover-content').style.display = 'none';">
<p>Hover</p></div>
</div>
// some other content here
<div class="hover-content" id="hover-content">
<p>hovered content</p>
</div>
notice that I added an id attribute to the hover-content div.
Try this, i think it will help you :
<script>
$(document).ready(function () {
$( ".hover-me" ).mouseenter( function () {
$( ".hover-content" ).show();
}).mouseout(function () {
/*anything you want when mouse leaves the div*/
} );
});
</script>
So you want to display the .hover-content when you hover the test. You can try the following solution. If it does not work, you gotta use javascript to check for the mouseover event. Hope it helps!
.test:hover ~ .hover-content {
display: block;
}
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>
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.
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();
});
});
I have two divs div1 and div2. I want div2 to be automatically hidden but when i click on preview div then div2 to be made visible and div1 to hide. This is the code i tried but no luck :(
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("div2").hide();
$("#preview").click(function() {
$("#div1").hide();
$("#div2").show();
});
});
</script>
<div id="div1">
This is preview Div1. This is preview Div1.
</div>
<div id="div2">
This is preview Div2 to show after div 1 hides.
</div>
<div id="preview" style="color:#999999; font-size:14px">
PREVIEW
</div>
Make sure to watch your selectors. You appear to have forgotten the # for div2. Additionally, you can toggle the visibility of many elements at once with .toggle():
// Short-form of `document.ready`
$(function(){
$("#div2").hide();
$("#preview").on("click", function(){
$("#div1, #div2").toggle();
});
});
Demo: http://jsfiddle.net/dJg8N/
This is an easier way to do it. Hope this helps...
<script type="text/javascript">
$(document).ready(function () {
$("#preview").toggle(function() {
$("#div1").hide();
$("#div2").show();
}, function() {
$("#div1").show();
$("#div2").hide();
});
});
<div id="div1">
This is preview Div1. This is preview Div1.
</div>
<div id="div2" style="display:none;">
This is preview Div2 to show after div 1 hides.
</div>
<div id="preview" style="color:#999999; font-size:14px">
PREVIEW
</div>
If you want the div to be hidden on load, make the style display:none
Use toggle rather than click function.
Links:
JQuery Tutorials
http://docs.jquery.com/Main_Page
http://www.w3schools.com/jquery/default.asp (W3Schools)
http://thenewboston.org/list.php?cat=32 (Video Tutorials)
http://andreehansson.se/the-basics-of-jquery/ (Basic Tutorial)
JQuery References
http://api.jquery.com/
http://oscarotero.com/jquery/
You are missing # hash character before id selectors, this should work:
$(document).ready(function() {
$("#div2").hide();
$("#preview").click(function() {
$("#div1").hide();
$("#div2").show();
});
});
Learn More about jQuery ID Selectors
The second time you're referring to div2, you're not using the # id selector.
There's no element named div2.
$(document).ready(function() {
$('#div2').hide(0);
$('#preview').on('click', function() {
$('#div1').hide(300, function() { // first hide div1
// then show div2
$('#div2').show(300);
});
});
});
You missed # before div2
Working Sample
At first if you want to hide div element with id = "abc" on load and then toggle between hide and show using a button with id = "btn" then,
$(document).ready(function() {
$("#abc").hide();
$("#btn").click(function() {
$("#abc").toggle();
});
});