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.
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:
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;
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With