Button press effect not working in css - javascript

I want to have this button pressed effect in css. I mean for example lets say I press a button then I want to change its css so that it looks pressed. Here is something that I tried. But it's not working. I used example from a site. But the button's size gets smaller and it looks different. Here is the link for the code http://jsfiddle.net/goku/GdD34/
.pressed{
position:relative;
top: 3px;
color: #fqq;
box-shadow: none;
-moz-box-shadow: none;
-webkit-box-shadow: none;
}
input.happy {
background-image: url(/img/happy.png);
background-color: transparent;
background-repeat: no-repeat;
border: none;
width: 64px;
height: 64px;
margin: 10px;
cursor: pointer;
border-radius:8px;
-moz-border-radius:8px;
-webkit-border-radius:8px;
box-shadow: 0px 3px 5px #000;
-moz-box-shadow: 0px 3px 5px #000;
-webkit-box-shadow: 0px 3px 5px #000;
}
$('.happy').click(function() {
alert('hello');
$('.happy').attr('class','pressed');
});
<input type="button" class="happy">

Just used the :active pseudo-class.
input.happy:active { /* Your style */ }

This is happening because you're replacing the class and not adding a new one. you should use :
$('.happy').addClass('pressed');
Instead of :
$('.happy').attr('class','pressed');
Because when u do that you remove all the css you previously applied to it. Your other option it to add the width/height or any other css to the pressed class.

There are a few things in your code (fiddle):
I guess you want to use a javascript framework (like jQuery), you did not select one in the fiddle.
You have a typo in the fiddle, inside the function it says $('happy') so no element will be found.
You remove the class "happy" within the javascript and replace it with pressed. Maybe you want to apply both $('.happy').attr('class', 'happy pressed'); But then for change .pressed to input.pressed and move below .happy
Perhaps you don't want all buttons to change, use use $(this).attr(...) inside the function

I'd suggest you change the order of your CSS, the and the JS to:
<style>
input.happy {
background-image: url(/img/happy.png);
background-color: transparent;
background-repeat: no-repeat;
border: none;
width: 64px;
height: 64px;
margin: 10px;
cursor: pointer;
border-radius:8px;
-moz-border-radius:8px;
-webkit-border-radius:8px;
box-shadow: 0px 3px 5px #000;
-moz-box-shadow: 0px 3px 5px #000;
-webkit-box-shadow: 0px 3px 5px #000;
}
input.happy.pressed{
position:relative;
top: 3px;
color: #fqq;
box-shadow: none;
-moz-box-shadow: none;
-webkit-box-shadow: none;
}
</style>
<script>
$(function(){
$(".happy").click(function(){
$(this).addClass("pressed");
});
});
</script>
<input type="button" class="happy">
Note, the "$(function(){" bit says "do this after page load". "addClass" will add the class to the list of classes for an element, but the event must be assigned after the DOM has loaded.
Also, you must use '$(this)' instead of '$(".happy")' inside the click function as to only apply the style to the button that was clicked.

You had some syntax errors.
Best event for this isn't .click(), its .mousedown();
When you click the Button without Releasing:
$('.happy').mousedown(function() {
$('.happy').attr('class','pressed');
});
I believe now it's working : http://jsfiddle.net/HKZ7M/
Then when you release the mouse, give it back the old class.
When you Click the Button then Release it
$('.happy').mousedown(function() {
$('.happy').attr('class','pressed');
$('.pressed').mouseup(function() {
$('.pressed').attr('class','happy');
});
});
It's working : http://jsfiddle.net/Xx2Gn/
Important Note: The .pressed button is smaller than the .happy button, when you release the mouse you have to make sure that the pointer will be above the new .pressed button, that's why you must make them the same size.

Related

How do I style the "no file chosen" tooltip for file upload buttons in order to position it below the button?

I have managed to remove the tooltip entirely, just to see if it was possible to style this tooltip, in any way. But what I actually need is to position it centred below the choose file button.
The CSS for getting rid of the tool tip was basically this:
input[type="file"] {
display: none;
}
Which basically results in the button looking like this
However, if I remove the css, I am left with this mad button inside of a button mess which includes the tooltip inside the button
To be clear, both the button and the button within the button do the same thing. I am not 100% clear on why it displays like it as this is my first time working with a file upload button which isn't my own code.
Followed by this Codepen you can simply do it like this:
<html>
<style>
.custom-file-input {
color: transparent;
}
.custom-file-input::-webkit-file-upload-button {
visibility: hidden;
}
.custom-file-input::before {
content: 'Select some files';
color: black;
display: inline-block;
background: -webkit-linear-gradient(top, #f9f9f9, #e3e3e3);
border: 1px solid #999;
border-radius: 3px;
padding: 5px 8px;
outline: none;
white-space: nowrap;
-webkit-user-select: none;
cursor: pointer;
text-shadow: 1px 1px #fff;
font-weight: 700;
font-size: 10pt;
}
.custom-file-input:hover::before {
border-color: black;
}
.custom-file-input:active {
outline: 0;
}
.custom-file-input:active::before {
background: -webkit-linear-gradient(top, #e3e3e3, #f9f9f9);
}
</style>
<input type="file" class="custom-file-input" />
</html>
Now the question is how it works?
To get the answer know about css ::before selector here
Hope this helps!

Style a a href as a button

I'm creating a large set of HTML components that works in every browser (where did the idea started anyway :-) )
Now, I want to have a button, and according to this post on StackOverflow, I should not use a button because that one has a 3D push effect on click. In order to remove that one, the advice was to use a a href and style that to the button I like.
So here's the HTML:
<a href="#" class="button">
<span>Yes</span>
</a>
And off course, here's the HTML:
a.button {
color: #444;
border: 1px solid #ababab;
cursor: default;
padding: 0 5px 0 5px;
text-decoration: none;
}
a.button:hover {
background-color: #cde6f7;
border: 1px solid #92c0e0;
}
a:active.button {
background-color: #92c0e0;
border: 1px solid #2a8dd4;
}
Nothing really commplicated
Now, this does all work in Google Chrome and Firefox as this JsFiddle demonstrates.
The button has 3 different states:
A normal 'default' button.
A style when you hover on it.
A style when you click on it.
Now, Internet Explorer does not apply a new style when you click on the button, it's the same style as the one on hovering. Unless you click the border (If you manage to click the border, than the correct style does apply).
Now, why do I have this behaviour and can it be solved as it is crucial to the development of my Control Suite.
I know it's possible to solve with jQuery by adding a removing a class when you click on it, but this seems a very ugly solution and if there's a 'CSS-Friendly' solution, I would like to use that one.
This may be because the CSS selector is backwards:
Change:
a:active.button {
to
a.button:active {
Chrome et al don't appear to give a care about what order these are in, but IE is, well, IE.
a.button {
color: #444;
border: 1px solid #ababab;
cursor: default;
padding: 0 5px 0 5px;
text-decoration: none;
}
a.button:hover {
background-color: #cde6f7;
border: 1px solid #92c0e0;
}
a.button:active {
background-color: #92c0e0;
border: 1px solid #2a8dd4;
}
<a href="#" class="button">
<span>Yes</span>
</a>
Edit
The issue appears to be that when you click on the link, you are actually clicking the span and, in IE, the click event is not bubbling. As far as IE is concerned, the anchor is not being :activeated.
You need to take the span out of the anchor:
a.button {
color: #444;
border: 1px solid #ababab;
cursor: default;
padding: 0 5px 0 5px;
text-decoration: none;
}
a.button:hover {
background-color: #cde6f7;
border: 1px solid #92c0e0;
}
a.button:active {
background-color: #92c0e0;
border: 1px solid #2a8dd4;
}
<a href="#" class="button">
Yes
</a>
Edit
If you need the span, then the only solution left is a javascript one.
This block of code adds a mousedown/mouseup event listener to all .button elements which toggles the active class on/off.
// vanilla JS
var anchors = document.getElementsByClassName('button');
for (var i = 0; i < anchors.length ; i++) {
anchors[i].addEventListener("mousedown", function (event) {
this.classList.add('active');
}, false);
anchors[i].addEventListener("mouseup", function (event) {
this.classList.remove('active');
}, false);
}
// jQuery
jQuery(document).ready(function($) {
$('a.button').mousedown(
function(){
$(this).addClass('active');
}
)
.mouseup(
function(){
$(this).removeClass('active');
}
);
});
And we change the :active line of the css to:
a.button:active,
a.button.active {
background-color: #92c0e0;
border: 1px solid #2a8dd4;
}
Which listens to both the :active pseudo-class, as well as the .active class.
//pure JS solution
var anchors = document.getElementsByClassName('button');
for (var i = 0; i < anchors.length ; i++) {
anchors[i].addEventListener("mousedown", function (event) {
this.classList.add('active');
}, false);
anchors[i].addEventListener("mouseup", function (event) {
this.classList.remove('active');
}, false);
}
//jQuery solution
/*
jQuery(document).ready(function($) {
$('a.button').mousedown(
function(){
$(this).addClass('active');
}
)
.mouseup(
function(){
$(this).removeClass('active');
}
);
});
*/
a.button {
color: #444;
border: 1px solid #ababab;
cursor: default;
padding: 0 5px 0 5px;
text-decoration: none;
}
a.button:hover {
background-color: #cde6f7;
border: 1px solid #92c0e0;
}
a.button:active,
a.button.active {
background-color: #92c0e0;
border: 1px solid #2a8dd4;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<a href="#" class="button">
<span>Yes</span>
</a>
This seems to be a simple problem of priority... a.button:hover is more accurate than a:active.button so it has precedence. The reason why the browsers don't all behave exactly the same is simply because they handle ties differently.
Making sure that the different pseudo classes are always set at the same level of the selector rule will help counter this problem.
So, this means a:active.button should be switched to a.button:active or the others be switched...
You can remove push effect on button by adding a
button {
padding:0;
}

Applying a CSS transition when adding a class via JS

I am creating the a button, that when clicked, will reveal an email input for the user to fill out. This is working, I just want a smooth transition so the action is not "jerky".
This is the HTML for the button and form
<button id="work" onclick="workyes()">Sign Up!</button>
<div id="workyes" class="invisible">
<form><input type='email'></form>
</div>
The CSS I'm Using:
button {
border: 0;
background: #5786c1;
color: white;
padding: 8px 14px;
font-weight: bold;
font-size: 18px;
text-decoration: none;
display: inline-block; /* needed for anchors */
position: relative;
box-shadow: 1px 0px #3a587f, 0px 1px #4171ae,
2px 1px #3a587f, 1px 2px #4171ae,
3px 2px #3a587f, 2px 3px #4171ae,
4px 3px #3a587f, 3px 4px #4171ae,
5px 4px #3a587f, 4px 5px #4171ae,
6px 5px #3a587f, 5px 6px #4171ae;
-webkit-transition: all 1s; /* For Safari 3.1 to 6.0 */
transition: all 1s;
max-height: inherit;
}
button:hover {
box-shadow: none;
-webkit-transition: all 1s; /* For Safari 3.1 to 6.0 */
transition: all 1s;
}
.invisible {
display: none;
}
.visible {
display: block;
}
And the JS that ties it all together:
function workyes() {
var box = $('#work');
var form = $('#workyes');
box.toggleClass('invisible');
form.toggleClass('visible');
}
I have an on hover transition that works but I just can't get one to work when the .invisible or .visible classes are added to the script.
Is there any way to make the CSS transitions work or add an effect in a different.
I think you can add a second argument to the toggleClass(); function that specifies duration to smooth out the transition:
function workyes() {
var box = $('#work');
var form = $('#workyes');
box.toggleClass('invisible', 1000);
form.toggleClass('visible', 500);
}
Alternatively you can use the fadeToggle();, slideToggle(); or fadeIn(); / fadeOut(); functions.
Note - you should probably use jQuery's click function rather than the onclick html attribute.
HTML
<button id="clickToShowForm">Sign Up!</button>
<div class="hidden">
<form><input type='email'></form>
</div>
CSS
.hidden {
display:none;
}
jQuery
$('clickToShowForm').click(function(){
$('hidden').fadeToggle();
});
https://api.jquery.com/toggleClass/
http://api.jquery.com/fadetoggle/
maybe you can try to use the fadeIn and fadeOut functions from jquery, and when the animation is finished, add/remove the class? (Although not really necessary anymore..)
<button id="work">Sign Up!</button>
<div id="workyes" class="invisible">
<form id="workyes">
<input type='email'>
</form>
</div>
you better use the jquery selectors instead of an 'onclick' event on the element itself.
$("#work").click(function () {
$(this).fadeToggle("slow", function () {
$(this).toggleClass('invisible');
$('#workyes').fadeToggle("slow", function () {
$('#workyes').toggleClass('visible').focus();
});
});
});
I would think the toggleClass lines are not necessary anymore? Maybe you use there classes for other reasons.. so i left them in...
Take a look here for demo : https://jsfiddle.net/bff1y83b/

Adding a drop shadow to elements not yet clicked

Based on the following tiled layout: http://jsfiddle.net/bzCbh/7/
Could anyone suggest a solution for adding a drop shadow to the unclicked element so it appears as though there's depth under the tiles ?
Thanks
** Apologies, the solution here lay in adding a class rule containing box shadow only to the current tile layer & also: .layer .tile img { position: relative;} Position Relative stopped the box-shadow overlapping onto neighbouring elements. **
This can be done with CSS:
.element{
box-shadow:0 0 10px #999;
}
.element.clicked{
box-shadow:0 0 0 #999
}
And the JS:
$('.element').on('click', function(){
$(this).addClass('clicked');
}
This is straight CSS3... make sure you include browser prefixes for cross-browser compatibility.
Check out the jQuery Shadow Plugin http://syddev.com/jquery.shadow/
Depending on the browsers you need to support, you could use the CSS3 Box-Shadow property:
.tile {
-moz-box-shadow: 3px 3px 4px #000;
-webkit-box-shadow: 3px 3px 4px #000;
box-shadow: 3px 3px 4px #000;
}

how is the search mail button implemented?

I am using Firebug, I just saw tha it is a div with css, but I dont get it how they did it?
<div id=":ri" class="J-Zh-I J-J5-Ji L3 J-Zh-I-Js-Zq" tabindex="0" role="button" style="-moz-user-select: none;">Search Mail</div>
I am trying to make something similar but I am just a beginner,I want that effect of the button but I don't get it how they did it? even I don't understand the css, I just copy this but no effect
.num1 {
-moz-border-radius: 2px 2px 2px 2px;
background: -moz-linear-gradient(center top , #F5F5F5, #F1F1F1) repeat scroll 0 0 transparent;
border: 1px solid rgba(0, 0, 0, 0.1);
color: #666666;
cursor: default;
font: 75% arial,sans-serif;
margin: 0 8px 0 0;
outline: medium none;
padding: 3px 12px;
text-align: center;
vertical-align: middle;
white-space: nowrap;
}
.num2{
display: inline-block;
position: relative;
}
.num3{
-moz-border-radius-bottomleft: 0;
-moz-border-radius-topleft: 0;
border-left-width: 0;
margin-left: 0 !important;
}
Here just the CSSed div: http://jsfiddle.net/bmWGY/1/
You'll need much more if you want to do something with this div.
Gmail uses JavaScript to detect the click event on the div. In addition, classes are dynamically added/removed to give the "button" the correct styles.
It is much easier to style a div element correctly than to try to style input and button elements for a cross-browser solution.
It's likely a simple div with a javascript onclick function attached. If using jQuery or some other framework, the "action" can be defined elsewhere using the .click() or .bind() (for jQuery) functions. See the examples provided in the preceding two links to see this in action.

Categories

Resources