Customize without Altering Animations
Check out this JSFiddle link for reference.
If the goal is to modify the background image and shape only, CSS can be utilized:
.button {
height:30px;
width:60px;
-webkit-border-radius:15px;
-moz-border-radius:15px;
border-radius:15px;
background-image:url(...)
}
.button:hover {
-webkit-border-radius:0px;
-moz-border-radius:0px;
border-radius:0px;
background-image:url(...)
}
For text changes, JavaScript comes into play:
<style>
#button {
height:30px;
width:60px;
border-radius:15px;
background-image:url(img1.png);
}
</style>
<script>
function mouseOver() {
document.getElementById("button").style.backgroundImage = "url(img2.png)";
document.getElementById("button").style.borderRadius = "0px";
document.getElementById("button").innerHTML = "Submit";
}
function mouseOut() {
document.getElementById("button").style.backgroundImage = "url(img1.png)";
document.getElementById("button").style.borderRadius = "15px";
document.getElementById("button").innerHTML = "✓";
}
</script>
<div id="button" onmouseover="mouseOver()" onmouseout="mouseOut()">✓</div>
Incorporating Transition Effects
Explore this JSFiddle link for demonstration.
To animate the button effectively, include the following CSS properties:
-webkit-transition:all 0.2s;
transition:all 0.2s;
Additional classes like .fade-out and .fade-in can be added for more complex animations:
.fade-out {
opacity: 1;
animation: fadeOut 0.2s;
}
.fade-in {
opacity: 0;
animation: fadeIn 0.2s;
}
Introduce @keyframes animations in your CSS to handle fading effects smoothly.
@keyframes fadeOut {
from {opacity: 1;}
to {opacity: 0;}
}
@keyframes fadeIn {
from {opacity: 0;}
to {opacity: 1;}
}
The script then integrates JavaScript functions for a sophisticated user experience:
function mouseIsOver() {
btn = document.getElementById("button_java");
btn.childNodes[0].className += ' fade-out';
// More functionality here based on requirements
}
function mouseIsOut() {
// Functionality for mouse out event
}
- childNodes[0]: Retrieves inner children elements
- className += ' fade-out': Adds specified class without altering existing ones
- setTimeout(function,waitduration): Delays execution of code snippet
- className.replace(' fade-out',''): Removes targeted class from element's class list
Your Unique Button Design
View the customized button layout on JSFiddle or PasteBin
Enhance the button appearance with additional styles and jQuery integration:
.button p {
line-height:1em;
margin:0;
}
Remember to include jQuery library in your header section:
<script src="http://code.jquery.com/jquery-2.1.3.js"></script>
Add JavaScript code snippets for interactive hover actions:
<script type="text/javascript">
$( document ).ready(function() {
// Handle hover interactions here
});
</script>
With these adjustments, you can create a personalized and visually appealing button for your website!