div on click not fired during transition - html

I have a simple div with an onclick function (Note I removed the content of the svg for readability):
<div id="sidebar-toggler">
<svg></svg>
</div>
body.on('click', '#sidebar-toggler', function() {
$('#sidebar').toggleClass('collapsed');
});
this is working without a problem. But the div has also a transition to move a little to the right on hover:
&:hover
left: 30px
transition: left 0.4s ease-out
Now, sometimes (quite often) during the transition, the click event is not fired. Is this a common Problem and is there a solution to this?
What I have tried is to make a wrapper div around it, which is as big as the animations goes. But the Problem still exist.
What I have done is to add a wrapper around my round div like this:
( ) = Round div (button)
_________
| | = wrapper
_________
transition
From:
_________
|( ) |
_________
To:
_________
| ( )|
_________
And of course, I remapped the onclick to the wrapper. I though because the onclick is on the wrapper, and there is no transition on the wrapper, the onclick should be fired. But as said, no success so far.

This is because click is very sensitive: you must click your mouse both down and up without the element moving.
You could use a mousedown or mouseup instead:
body.on('mouseup', '#sidebar-toggler', function() {
$('#sidebar').toggleClass('collapsed');
});
That wouldn't be dependent on movement.
The same would happen if you have a onClick method on a non-moving element, but you move your mouse. It wouldn't work since it's a drag instead of a click.

Related

Opacity if the mouse is moving

I want to change the opacity of a div (like 0.5) when the mouse is moving
and when the mouse doesn't move change the opacity to 1
is that possible ?
Thank's.
Yves
If you want to apply this to the element whenever the mouse is on the window, try this.
HTML
<div id="myDiv" style='width:100px;height:100px;background:rgb(255,0,0);'></div>
JS:
$(window).on("mousemove",function(e){
$("#myDiv").css({opacity:0.5});
clearTimeout(window.myTimeout);
window.myTimeout=setTimeout(function(){
$("#myDiv").css({opacity:1});
},100);
});
Basically, every time you move the mouse, you create a timeout but in the next movement, you cancel it. When you stop moving the mouse, eventually the timeout will execute and change the opacity of the element.

Chrome vs. FF button click behavior

I used the :active pseudoclass on my buttons to change the padding and border color when clicked. This visually mimics a button press. See an example.
Works in FF. In Chrome, though, the clicks don't always register. Specifically, if you click-and-hold on the text inside the button, then drag off the text (but still inside the button), the click will not fire. Same goes if you click in the padding, then drag into the text.
I've read weird things about buttons and padding in FF vs. Chrome but I can't figure out how to solve this problem.
Solution: Give it a Single Thing to Click
Adding a position: relative to the button and then overlaying it with a :before pseudo element like so:
button:before {
content: '';
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
Seemed to resolve the bug for me. See this fiddle. I imagine it resolves the bug (which I do think it is a bug) by giving a single "element" to focus upon when it is clicked.
Try this one: http://jsfiddle.net/uPZuJ/9/
Instead of checking for both mousedown and mouseup on the same element, check only for mousedown on your button and then check for mouseup on any other element.
Your button works great in Chrome. I have fixed the "click-and-hold on the text inside the button" thing by few lines of CSS and a attribute for IE.
http://jsfiddle.net/uPZuJ/2/
EDIT:
This is a screenshot of the bug I fixed:
Try this: http://jsfiddle.net/uPZuJ/22/ http://jsfiddle.net/uPZuJ/25/
This is how it works:
Initialize 'down' to false
mousedown on button causes 'down' to be true
On mouseup/click, if 'down' is true, append text 'click' and set 'down' to false
Clicking triggers a click.
Dragging the text into the padding triggers a click.
Dragging the padding into the text triggers a click.
Dragging from the inside to the outside does not trigger a click.
Dragging from outside to inside does not trigger a click.
Here is the code:
var down=false;
$("button").mousedown(function() {
down=true;
});
$("button").mouseup(function() {
if(down) {
$("#clicks").append("<div>click</div>");
}
down=false;
});
$("button").click(function() {
if(down) {
$("#clicks").append("<div>click</div>");
}
down=false;
});
After having playing around with your example, I think I got what happen behind the scene. By default click event will check both mousedown and mouseup event to be fired before it fires the callback. The problem is that you click on the text, your current target is the text now(it means mousedown event is already fired inside the text area, and it's waiting for mouseup event to be fired in order to complete a click event). However, you drag cursor out of the text and release mouse outside the text area, and your current target now is the Button, not the text anymore(mouseup event doesn't fire inside text area, but inside button area), so click event won't happen. However, if you click on the text and drag, and release your mouse inside text area, it will works just fine. This is the problem of event bubbling or capturing. In jQuery we have event.stopPropagation() or event.stopImmediatePropagation, but it just works for FF, not in Chrome.
What I suggest is using mousedown and mouseup event instead of click event.
Use "mouseup" instead of "click", it should work in ie, chrome and ff.
var elementName = "";
$("button").mouseup(function() {
if (elementName != "" && elementName == "BUTTON") {
$("#clicks").append("<span>click</span><br/>");
elementName = "";
}
});
$("button").mousedown(function() {
elementName = $(this).get(0).tagName;
});

Zoom in Zoom out loop on action script 3

I have this code for my effect to zoom in and zoom out in certains buttons
canada.addEventListener(MouseEvent.MOUSE_OVER, canadaover);
function canadaover(event:MouseEvent):void
{
gotoAndPlay("canadaS");
trace("in");
}
canada.addEventListener(MouseEvent.MOUSE_OUT, canadaout);
function canadaout(event:MouseEvent):void
{
gotoAndPlay("canadaF");
trace("out");
}
canada.addEventListener(MouseEvent.CLICK, clickcanada);
function clickcanada(event:MouseEvent):void
{
trace("Mouse clicked");
}
the problem is when u reach certain corner of the button it kinda gets into a loop, any ideas how can i fix this?
here its the link of the swf i'm trying to do:
http://viajescupatitzio.com/america%20map.swf
If your buttons are MovieClips you can add inside a layer with a mask (for example rectangle) on top. Mask width and height should be your mouseover region and give it alpha = 0. It will be invisible, but it will work with MOUSE_OVER and MOUSE_OUT Events.
You should move your buttons into a different hierarchy level than the graphics you are changing - even if the buttons disappear, or are covered with graphics for just a very short moment, both mouseOver and mouseOut events will be fired (the mouse has left and reentered the button) - and that probably causes your "loop".
It is generally a good idea to have animations and graphical objects within nested MovieClips, and place control elements on a higher level of the display list - that way you can make sure the elements don't overlap and/or interfere.

Mouse Overs with Divs

I am trying to get this mousever to work, but I seems to be acting very buggy in all browser versions. I have something like this
<div id="foo" onMouseOut="makeHidden('foo');">Link Text</div>
I don't want the div to be hidden when the mouse goes over the link, and I assumed it wouldn't because the link is in the div. How can I get the div to stay visible until the mouse leaves it's boundary.
You might want to look in to using jQuery http://jquery.com/
Then you could write something like this:
$("#foo").mouseenter( function(){makeHidden('foo');} );
$("#foo").mouseleave( function(){makeVisible('foo');} );
Or just cut out the middle man
$("#foo").mouseenter( function(){$(this).css("visibility", "hidden");} );
$("#foo").mouseleave( function(){$(this).css("visibility", "visible");} );
By using a fancy bubbling trick. See: http://jsfiddle.net/minitech/kZcCr/
You want to stop the propagation of the mouseout event if it's being applied to an element's children, and you also want to cancel the mouseout of the parent if we're moving into one of its children. That can be done using relatedTarget, or toElement on IE.
actuallly i m not geeting ur point might be this code will help u
<script type="text/javascript">
function abc (mylink){
document.getElementById('mylink').style.display = 'none';
}
function abcd(mylink){
document.getElementById('mylink').style.display = 'block';
}

Hide partial div - toggle open on click

I know how to toggle an entire div, however I only want to hide all but the top 10% or top 100px, for example. And then when the div is clicked, the entire div opens.
I thought I saw this a while ago, but can't remember where.
Thanks.
$(document).ready(function() {
// hides the slickbox as soon as the DOM is ready
$('#slickbox').hide();
// toggles the slickbox on clicking the noted link
$('#slick-toggle').click(function() {
$('#slickbox').toggle(400);
return false;
});
});
Your code should be something in the lines of:
$(document).ready(function() {
// hides the slickbox as soon as the DOM is ready
$('#slickbox').animate({height: '20px'});
// toggles the slickbox on clicking the noted link
$('#slick-toggle').click(function() {
$('#slickbox').animate({height: '100%'});
return false;
});
});
Take a look the image on my home page, is this kind of what you want to do?
http://www.carsonshold.com/
I have it jet out when you hover over it, but that can easily be changed to a click. It somewhat complicated to do, and still isn't perfect in IE (the page loads and the clip isn't recognized until you hover over it).
It may be slightly different from what you want since I did this on an image rather than a div, so I needed to animate the clipping mask. The function I used is as follows:
var featureDuration = 300; //time in miliseconds
$('#featured-img').hover(function() {
$(this).animate({ left : "-164", clip: "rect(0px,384px,292px,0px)" },{queue:false,duration:featureDuration});
}, function() {
$(this).animate({ left : "17px", clip: "rect(0px,203px,292px,0px)" },{queue:false,duration:featureDuration});
});
If you want to animate the clip, you will need to insert this JS as well because it doesn't behave properly otherwise. http://www.overset.com/2008/08/07/jquery-css-clip-animation-plugin/
Take a look at the CSS in my code if you are unsure how I did the rest of it, or comment on here if you have any questions.
Cheers
Did this rather quickly, note it will only hide the bottom portion.
http://jsfiddle.net/loktar/KEjeP/
Simple toggle that changes the height, hiding the rest of the content within. Easy enough to animate as well, just modify the toggle functions to adjust the heights rather than adding a class.