Dropdown menu after :hover effect - html

I would like to achieve the following effect in my dropdown menu, using only CSS and HTML?
Basically the idea is when move the mouse outside the dropdown menu, it keeps open till you click outside the menu or after X seconds. Is it possible to achieve this without any extra libraries or technologies (besides CSS and HTML)?

UPDATE: Now that the question is clarified, it would seem the behavior that you are seeking is not possible without using JavaScript. I did find an example of a pure CSS clickable dropdown, but it does not meet all of your criteria.
--- Original answer before question was clarified ---
You've actually asked two questions here. You originally did not specify a preferred technology to accomplish this, so I found answers to each using jQuery. (These answers use .show() and .hide() to show/hide the element - but you could also use .css() to directly change the CSS yourself, if you prefer.)
Dropdown keeps open until click outside the menu
You can detect a click on the document and then check for immediate ancestry to see if the desired element was clicked. (Partial credit: https://stackoverflow.com/a/3028037/561309)
$(document).click(function(event) {
if($(event.target).closest('#menucontainer').length) {
if($('#menucontainer').is(":hidden")) {
$('#menucontainer').show();
}
}
if(!$(event.target).closest('#menucontainer').length) {
if(!$('#menucontainer').is(":hidden")) {
$('#menucontainer').hide();
}
}
})
Dropdown keeps open until certain time has passed
You can use the setTimeout function to have the CSS change after a certain period of time - example here is two seconds. The .stop method should keep the timeout from executing once the mouse moves back over the menu, but this should be tested. (Partial Credit: https://stackoverflow.com/a/14247096/5365001)
$(document).ready(function(){
$("#menucontainer").click(function(){
$("#menucontainer").toggle());
});
$("#menucontainer").mouseout(function(){
setTimeout(function(){ $("#menucontainer").hide(); },2000);
});
$("#menucontainer").mouseover(function(){
$("#menucontainer").stop(); });
});
});
And, if you wanted to combine both, this should do the trick:
$(document).ready(function(){
$(document).click(function(event) {
if($(event.target).closest('#menucontainer').length) {
if($('#menucontainer').is(":hidden")) {
$('#menucontainer').show();
}
}
if(!$(event.target).closest('#menucontainer').length) {
if(!$('#menucontainer').is(":hidden")) {
$('#menucontainer').hide();
}
}
})
$("#menucontainer").mouseout(function(){
setTimeout(function(){ $("#menucontainer").hide(); },2000);
});
$("#menucontainer").mouseover(function(){
$("#menucontainer").stop(); });
});
});

Related

Can't highlight text

Check http://discoveryobstacleruns.nl/. I can't highlight any text on the site, but it doesn't seem to be a z-index issue and it doesn't have any other means to block text highlighting. Any idea what's causing it?
Edit: disabling JS seems to do the trick, which leads to believe it's Google Maps causing it. However, all elements above the map are clickable, the content is clearly on top of the map so why wouldn't I be able to highlight text?
So, apparently
document.getElementById('map').bind('click', function(e) {
if(e.stopPropagation) {
e.stopPropagation();
}
});
fixes it but gives an error message in console. Which makes sense because it should be:
document.getElementById('map').addEventListener('click', function(e) {
if(e.stopPropagation) {
e.stopPropagation();
}
});
but that doesn't work at all (doesn't give an error though). What gives??

How to make the entire area of a div clickable while excluding the checkbox that's on top of that area?

I have a div with a checkbox inside the div:
<div class="study-box set-box">
<span class="set-box-title"><i class="icon-th-list icon-white"></i>test set</span>
<input class="study-box-checkbox" type="checkbox" />
</div>
I want the entire div to be clickable, so I can perform an onClick() event. I don't want this event to be called when the checkbox is clicked, though. I'm thinking about adding some areas to the left and bottom of the checkbox and binding the event to those areas, but this seems hackish. Thoughts?
JSFiddle with a sample box/checkbox:
http://jsfiddle.net/PTSkR/59/
Stop event propagation when the input is clicked.
$(".study-box").on("click", function () {
console.log("clicked");
});
$("input").on("click", function (e) {
e.stopPropagation();
});
http://jsfiddle.net/PTSkR/60/
A simple solution would be to create a wrapper and set that to position: relative (removing position: relative from the study box), and move the checkbox outside of the study box. It will still be positioned in the same way, but won't be affected by hovering and clicking on the study box.
When a click event happens check the target.
$(document).ready(function () {
$('div.study-box').click(function (e) {
console.log(e.target.tagName);
if(e.target.tagName == 'DIV'){
// do something
} else {
// don't
}
});
});
i don't suggest jQuery when it can be simply done by using HTML/CSS. The trick is, place the checkbox outside of that div and push it visually inside the div using CSS position property. If you want to learn about CSS Position check out the following URL.
http://www.tutorialrepublic.com/css-tutorial/css-position.php

using target in css to keep color

I have the following example in http://jsfiddle.net/uA97K/
What I am trying to achieve is to keep the same colour on a selected tab as the hover. So when a user clicks on a tab, that selected tab will remain blue until another tab is selected.
I thought this could be done by using a:target but does look to be working.
#bar a:target { background: #00A3EF; color: #003366;}
Any ideas what I may be doing wrong?
With only CSS, you can't do it. But you can use jQuery, and an .active class for this:
http://jsfiddle.net/uA97K/1/
$('#bar a').click(function(){
$('.active').removeClass('active');
$(this).addClass('active')
});
This is, as already noted, impossible with CSS currently. It is, however, possible with plain JavaScript (albeit the following demonstration works only with browsers that support document.querySelector(), addEventListener() and the element.classList API):
function hashMonitor() {
var D = document,
active = D.querySelector('a.active'),
link = D.querySelector('[href="#' + D.querySelector(':target').id + '"]');
if (active) {
active.classList.remove('active');
}
link.classList.add('active');
}
window.addEventListener('hashchange', hashMonitor, false);
JS Fiddle demo.
Conceivably, under Level 4 of CSS (currently entirely unsupported in the wild) this could become possible, but until implementations appear it seems fruitless to speculate on how such selectors might be used.
References:
CSS Selectors, Level 4.
document.querySelector().
element.classList.
EventTarget.addEventListener().

Clickable DIVs using jQuery hide()

I have a working click-able, collapsible div script:
$(document).ready(function() {
$(".toggle-content").hide();
$(".byline").click(function() {
$(".toggle-content").hide();
$(this).next(".toggle-content").slideToggle(500);
});
});
It started out just like this:
$(document).ready(function() {
$(".toggle-content").hide();
$(".byline").click(function() {
$(this).next(".toggle-content").slideToggle(500);
});
});
Thsi second example worked nicely, but it made it so the user could open all the divs and this made the page too tall. I added the hide() function, but now it's causing this other issue.
I would like to add functionality that when each div is clicked again, it actually closes it (hides it). Then, all divs would be closed (hidden) at this point. Currently, one div is always open (visible). I want both functions if possible...
I'm using accordion elsewhere (I know this could be used here) but I kind of need to get this going quickly so I'm not trying to implement the simpler script here. If I could just find a fix using the existing script, I'd be stoked.
EDIT
I've edited the fiddle to show the improved fix:
http://jsfiddle.net/nicorellius/gsDVS/
This should work. It will ignore hiding the content related to the clicked element and will slideToggle that div accordingly
$(document).ready(function() {
$(".toggle-content").hide();
$(".byline").click(function() {
var $next= $(this).next(".toggle-content");
$(".toggle-content").not($next).hide();
$next.slideToggle(500);
});
});

Alter parent from child?

In CSS you can do this:
nav:hover a {
But is there a way of changing nav when a is hovered?
Use the javascript event onHover
In jquery, it's something like that:
$("a").hover(function () {
$('#nav').css("color","red");
});
Coming Soon, to CSS
Explicit subjects in a selector are coming in CSS, but we'll have to wait just a bit longer. Soon you will be able to explicitly declare which element is the subject, for instance with your code:
$nav a:hover {
background: red;
}
This would change a nav's background to read when any of its anchor descendants are hovered.
Source: Selectors Level 4 » Determining the Subject of a Selector
Until this is implemented, you'll have to use JavaScript (or one of the tools built with it, such as jQuery, Mootools, etc) to accomplish a task like this.
Doing it with jQuery
You can accomplish this with jQuery, by adding and removing a class when any of the elements nested anchors are hovered or exited:
$("nav").on("mouseenter mouseleave", "a", function(e){
$(e.delegateTarget).toggleClass("hovered", e.type === "mouseenter" );
});​
Demo: http://jsfiddle.net/jonathansampson/EPRRy/1/
This it the most compatible way
$("a").hover(function () {
$(this).parent().css({color:"red"});
});
No there isn't a way to ascend elements with CSS. To do explicitly what you described, it would require some JS.
#ssx had it close, but not quite, to do what your are asking with JS (and I'm going to simplify and use jQuery).
$("nav a:hover").hover(function() {
$(this).parent().css({'color': 'red'});
}), function() {
$(this).parent().css({'color': 'black'})
});
This gives changes the color to read, then back to black when the hover loses focus.
There is no solution for this in CSS 2 (dont know about CSS 3).
Javascript solution is easy and answered by other members.
You can try LESS. Using LESS you can do some conditional styling on DOM.
It will soon be intoduced in CSS 4. This is 5 or 6th question of the day i have seen today. I think it should be soon implemented by browser vendors.