So I'm attempting to make an "over time" text changer which changes a word in a sentence each few seconds, that code runs fine thou when the text is actually changed, the width of the sentence changes instantly removing the smoothness from the website.
Structure:
<div id="big">
This is some <span id="change">text</span>
</div>
The word changing is #change. I've tried using CSS transitions for width as well as transition properties:
margin-top: 18%;
font-size: 70px;
font-family: 'Slabia 27px', 'Droid Serif', Arial;
>> text-align: center;
text-transform: uppercase;
>> -webkit-transition-property: width;
>> -webkit-transition-duration: 2s;
>> transition: width .8s ease;
Also my jQuery (calls APP.init()):
var APP = {
init: function() {
loopChangeWord = setInterval(function(){
var $change = $('#change');
var iteration = ['quick', 'efficiently', 'well', 'together'];
var current = $change.html();
for (var i = 0; i < iteration.length; i++) {
if (iteration[i] == current) {
if (i+1 > iteration.length-1) var nw = iteration[0];
else nw = iteration[i+1];
$change.fadeOut(500, function(){
$change.html(nw).fadeIn(500);
});
}
}
}, 4000);
}
};
In the end** I'm asking for a way for a smooth transition of the text being changed, I expected the width change would trigger the CSS animation thou was wrong!
I had to change the text value which is in your <span id="change">text</span> to match either one value of your var iteration = ['quick', 'efficiently', 'well', 'together']; array.
Here is the working demo: https://jsfiddle.net/5p36n34j/1/
Hope this helps!
Here's an example using transition: all .8s ease; on #change instead of transition: width .8s ease; on #big.
var fontSize = 10;
$('#big').click(function() {
fontSize = fontSize * 2.2;
$('#change').css('fontSize', fontSize + 'px');
})
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
#big {
margin-top: 18%;
font-family: 'Slabia 27px', 'Droid Serif', Arial;
text-align: center;
text-transform: uppercase;
font-size: 10px;
cursor: pointer;
display: inline-flex;
justify-content: center;
align-items: center;
border: solid black 1px;
padding: 20px;
}
#change {
transition: all .8s ease;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="big">
Click on this <span id="change">text</span>
</div>
Related
I'm new to coding so apologies, I'm trying to do this thing where a button displays a random fun fact when clicked, but I also wanted to add a feature that, if hovered for long enough, the button text changes up to 3 times (at 3s, 5s, and 8s) and then stays on the last hover (8s one) until clicked, where it comes back to the first non hovered button text. Here's what I was working with. also, if anyone knows of a way to disable antialiasing, that'd be amazing as well
edit: apparently im not the best at explaining. i was looking to change the button text, not the fun fact. fun facts would only appear when clicked, i want the button text (the "click me for a fun fact") to change into 3 other texts when hovered for long enough, so for example, text a would change into text b after 3 seconds, then text b would change into text c after 5 seconds have passed since the hovering started, and then into text d after 8 seconds of constant hovering (so it would only happen after a total of 8 hovering seconds, changing at 3s, 5s and 8s). after that it should stay as text d until clicked. once its clicked, it should return to text a ("click me for a fun fact") and display a random fun fact
<!DOCTYPE html>
<html>
<head>
<style>
button {
display: inline-block;
background-color: #c0c0c0;
border-top: 2px solid white;
border-left: 2px solid white;
border-right: 2px solid black;
border-bottom: 2px solid black;
color: black;
text-align: center;
font-size: 15px;
font-family: yay;
padding: 5px;
width: 150px;
transition: all 0.5s;
cursor: pointer;
margin: 5px;
}
button span {
cursor: pointer;
display: inline-block;
position: relative;
transition: 0.1s;
}
.button {
transition: 0.2s;
}
.button:hover {
transition-delay: 3s;
}
</style>
</head>
<body>
<script>
var quotes = [
'fun fact 1',
'fun fact 2',
'fun fact 3',
'fun fact 4',
'fun fact 5'
]
function newQuote() {
var randomNumber = Math.floor(Math.random() * (quotes.length));
document.getElementById('quoteDisplay').innerHTML = quotes[randomNumber];
}
</script>
<button onclick="newQuote()">click me for a fun fact</button>
<div id="quoteDisplay">
</div>
</body>
</html>
To count seconds, setInterval() is here to help, you can do something every x second(s).
In this case, we want to check if the user hovered for 3,5 and 8sec, so we change the timer every seconds, when it reaches what we want, we call newQuotes(), then stop recording time when our timer is over 8sec, or if the user is not hovering the button anymore.
var quotes = [
'fun fact 1',
'fun fact 2',
'fun fact 3',
'fun fact 4',
'fun fact 5'
]
var myButtonText = [
'a button text',
'another button text',
'one more button text'
]
var timer=0, timerIdle=false, interval, text=0;
function newQuote() {
document.getElementById("myButton").innerText = "click me for a fun fact";
text = 0;
timer = 0;
if(quotes.length > 0) {
var randomNumber = Math.floor(Math.random() * (quotes.length));
document.getElementById('quoteDisplay').innerHTML = quotes[randomNumber];
//Remove item to avoid repetition
const index = quotes.indexOf(quotes[randomNumber]);
if(index > -1){
quotes.splice(index, 1);
}
}
}
function hoverMe() {
if(!timerIdle){
// Do something every 1 second
interval = setInterval(function(){
if(timer>8){
//if timer goes over 8sec, stop doing something on hover
timerIdle = true;
} else {
timer++;
// if timer == 3,5 or 8 call newQuote();
if([3,5,8].indexOf(timer) > -1 ) {
console.log(timer)
document.getElementById("myButton").innerText = myButtonText[text];
text++;
}
}
}, 1000);
}
}
// stop the interval if user is not hovering the button
function mouseLeave() {
clearInterval(interval)
}
button {
display: inline-block;
background-color: #c0c0c0;
border-top: 2px solid white;
border-left: 2px solid white;
border-right: 2px solid black;
border-bottom: 2px solid black;
color: black;
text-align: center;
font-size: 15px;
font-family: yay;
padding: 5px;
width: 150px;
transition: all 0.5s;
cursor: pointer;
margin: 5px;
}
button span {
cursor: pointer;
display: inline-block;
position: relative;
transition: 0.1s;
}
.button {
transition: 0.2s;
}
.button:hover {
transition-delay: 3s;
}
<!DOCTYPE html>
<html>
<body>
<button id="myButton" onclick="newQuote()" onmouseover="hoverMe()" onmouseleave="mouseLeave()">click me for a fun fact</button>
<div id="quoteDisplay"></div>
</body>
</html>
You can try the below implementation with some explanation
var quotes = [
'fun fact 1',
'fun fact 2',
'fun fact 3',
'fun fact 4',
'fun fact 5'
];
var selectedQuote = ""
//shuffle to make it non-duplicated records
function shuffle(array) {
var i = array.length,
j = 0,
temp;
while (i--) {
j = Math.floor(Math.random() * (i + 1));
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
function newQuote() {
if (!selectedQuote) {
var randomNumber = Math.floor(Math.random() * (quotes.length));
selectedQuote = quotes[randomNumber]
}
document.getElementById('quoteDisplay').innerHTML = selectedQuote;
}
var timer = [3000, 5000, 8000]; //3s, 5s, 8s
var interval;
//generate quotes when a user hovers over the element
function mouseOver() {
var currentTimerIndex = 0;
var passedTime = 0;
var timeStack = 1000;
var shuffledQuotes = shuffle(quotes); //randomize quotes
interval = setInterval(function() {
passedTime += timeStack;
if (currentTimerIndex > timer.length - 1) {
clearInterval(interval);
interval = null;
return;
}
if (timer[currentTimerIndex] <= passedTime) {
document.getElementById('quoteButton').innerHTML = shuffledQuotes[currentTimerIndex];
selectedQuote = shuffledQuotes[currentTimerIndex];
currentTimerIndex++;
}
}, timeStack)
}
//stop quote generations
function mouseOut() {
if (interval) {
clearInterval(interval);
interval = null;
}
}
button {
display: inline-block;
background-color: #c0c0c0;
border-top: 2px solid white;
border-left: 2px solid white;
border-right: 2px solid black;
border-bottom: 2px solid black;
color: black;
text-align: center;
font-size: 15px;
font-family: yay;
padding: 5px;
width: 150px;
transition: all 0.5s;
cursor: pointer;
margin: 5px;
}
button span {
cursor: pointer;
display: inline-block;
position: relative;
transition: 0.1s;
}
.button {
transition: 0.2s;
}
.button:hover {
transition-delay: 3s;
}
<button onclick="newQuote()" onmouseover="mouseOver()" onmouseout="mouseOut()" id="quoteButton">click me for a fun fact</button>
<div id="quoteDisplay">
</div>
I am creating an application that trains my memory by memorising colours. Every 2 seconds, the colour of the box will change from one to another. However if it switches to the same colour, it becomes difficult to differentiate. I am hoping to implement a blink effect when it transits to another colour. I tried to use blink animation by adjusting the time but it does not work well. How can i implement with my current code?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<style>
*{
box-sizing: border-box;
padding: 0;
margin: 0;
font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
}
#count {
font-size: 36px;
}
.section__hero {
height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 50px;
position: relative;
}
#countdownTimer {
position: absolute;
font-size: 72px;
left: 50%;
top: 50%;
transform:translate(-50%,-50%)
}
.section__btns {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 10px
}
#count,
#element,
#btn__answer,
#countdownTimer {
visibility: hidden;
}
#element {
height: 100px;
width: 100px;
background-color: #000;
border: 1px solid black;
}
#btn__action,
#btn__answer {
padding: 15px 30px;
border: none;
font-size: 18px;
color: #fff;
border-radius: 8px;
cursor: pointer;
/* display: block; */
}
#btn__action {
background-color: #332cf2;
}
#btn__answer {
background-color: #000;
text-decoration: none;
}
</style>
<title>Document</title>
</head>
<body>
<div class="section__hero">
<div id="countdownTimer"></div>
<div id="count"></div>
<div id="element"></div>
<div class="section__btns">
<button id="btn__action" onclick="action()">Start</button>
Answer
</div>
</div>
</body>
<script>
const colors = ["#000", "#fff", "#ffff00", "#ff0000"];
const btnsSect = document.getElementsByClassName("section__btns");
const recallSect = document.getElementsByClassName("section__recall");
const verfiySect = document.getElementsByClassName("section__verify");
const actionBtn = document.getElementById("btn__action");
const answerBtn = document.getElementById("btn__answer");
const element = document.getElementById("element");
const count = document.getElementById("count");
const countdownTimer = document.getElementById("countdownTimer");
let interval;
let answers = {};
let nextState = "Start";
let countdownValue = 4;
let elementCount = 0;
let isCountdown = false;
function action() {
switch (nextState) {
case "Start":
start();
break;
case "Stop":
stop();
break;
case "Reset":
reset();
break;
}
}
function start() {
nextState = "Stop";
actionBtn.innerHTML = nextState;
actionBtn.style.visibility = "visible";
element.style.visibility = "visible";
count.style.visibility = "visible";
changeElementColour();
interval = setInterval(changeElementColour, 2000);
interval = setInterval(changeElementColour, 2000);
}
function changeElementColour() {
const newElement = colors[Math.floor(Math.random() * colors.length)];
element.style.backgroundColor = newElement;
answers[elementCount] = newElement
elementCount++;
count.innerHTML = elementCount;
}
</script>
</html>
You can use animation:
#-webkit-keyframes blinker {
0% { opacity: 1.0; }
50% { opacity: 0.0; }
100% { opacity: 1.0; }
}
#element{
animation: blinker 2s cubic-bezier(0, 0, 0.9, -0.02) infinite;
}
Simply add a css animation that lets you render a transition between the inverse of the color that last the duration you want it to say 1/60 a second, and you may wish to apply additional details. You can trigger this to happen each time by simply changing toggling a temporary class to retrigger the animation.
CSS For Inverting the Color
From W3Schools
/* The animation code */
#keyframes example {
from {filter: inverse(1);}
to {filter: inverse(0);}
}
//make sure to add browser extensions for webkit
/* The element to apply the animation to */
div.class {
animation-name: example;
animation-duration: 0.3s;
animation-timing-sequence: ease-in-out;
}
This code was modified from W3Schools and needs to be adjusted to meet your exact application's needs.
Simply Toggle off/on The Class this css is placed on using Javascript and the animation should replay
Also Jquery and Javascript both have great API's for handling Animation Events as well that you may play around with.
See CSS Animations
Toggling Classes With JQuery
Getting Animation Events With JQuery
Per the code below, I'm able to create a Link with parameters that will navigate to my desired target page:
<style>
.fancy-link{
color: #FFFFFF;
text-decoration: none;
transition: all 0.3s linear;
-webkit-transition: all 0.3s linear;
-moz-transition: all 0.3s linear;
border-radius: 4px;
border: 1px solid black;
background: #0080FF;
padding-top: 0px;
padding-left: 5px;
padding-right: 5px;
padding-bottom: 0px;
width: 100px;
height: 10px;
font-size: 15px;
font-weight: bold;}
.fancy-link:hover{
color: #F44336;
padding-left: 10px;}
</style>
<html>
<a class="fancy-link" name="View" id="View"
href="'||'https://XXXXXX-sb1.app.website.com/app/common/custom/custrecordentry.nl?
rectype=142&id='||{internalid}||'"target="_blank">Check-In</a>
</html>
While this is useful, my intended goal is for this link to actually call the following button click event/element that lives # at the desired url (see href above), without actually navigating to that page. Is this possible? If so, some code examples would be very helpful.
.<input type="button" style="" class="rndbuttoninpt bntBgT"value="Check-In" id="custpageworkflow157" name="custpageworkflow157"onclick="try{
if (!!window) {
var origScriptIdForLogging = window.NLScriptIdForLogging;
var origDeploymentIdForLogging = window.NLDeploymentIdForLogging;
window.NLScriptIdForLogging ='CUSTOMSCRIPT_STICK_USER_PREFERENCE';
window.NLDeploymentIdForLogging= 'CUSTOMDEPLOY_STICK_USER_PREFERENCE';
}
try{
NS.Workflow.buttonClick('custpageworkflow157');
}
catch(e){
document.location = addParamToURL(addParamToURL(addParamToURL(document.location.href,'workflowbutton','157'),'workflowbuttoninstanceid','84083'),'workflowbuttonobsoletehandling','T');
}
}
finally{
if (!!window) {
window.NLScriptIdForLogging = origScriptIdForLogging;
window.NLDeploymentIdForLogging = origDeploymentIdForLogging;
}
}
;
return false;
"onmousedown="this.setAttribute('_mousedown','T');
setButtonDown(true, false, this);
" onmouseup="this.setAttribute('_mousedown','F');
setButtonDown(false, false, this);
" onmouseout="if(this.getAttribute('_mousedown')=='T') setButtonDown(false,false, this);
" onmouseover="if(this.getAttribute('_mousedown')=='T') setButtonDown(true, false, this);
" _mousedown="F">
You should be able to get away with this with a simple event listener in the button you are creating:
document.getElementById('custpageworkflow157).click();
So basically something like this:
document.getElementById("View").addEventListener("click", function(){
document.getElementById('custpageworkflow157).click();
});
I have a few controls that I am attempting to encapsulate on my webpage. I have tried a few different methods on encapsulating my controls and they have not succeeded. I tried using a div and this did not work too well and I have also tried this post:
Create a group box around certain controls on a web form using CSS
What is happening is that a box is being created but it is at the top of my webpage instead of around the controls.
I would like to create a grey box similar to the ones found on this webpage:
https://img.labnol.org/di/trigger1.png
Below, I am attaching a copy of the CSS and HTML code that I am using in order to create my form. The form is a simple file upload form that I tweaked from an example. I am using this on my own, personal website.
Here is the HTML:
<!DOCTYPE html>
<html>
<head>
<script>
/* Script written by Adam Khoury # DevelopPHP.com */
/* Video Tutorial: http://www.youtube.com/watch?v=EraNFJiY0Eg */
function _(el){
return document.getElementById(el);
}
function uploadFile(){
var file = _("file1").files[0];
// alert(file.name+" | "+file.size+" | "+file.type);
var formdata = new FormData();
formdata.append("file1", file);
var ajax = new XMLHttpRequest();
ajax.upload.addEventListener("progress", progressHandler, false);
ajax.addEventListener("load", completeHandler, false);
ajax.addEventListener("error", errorHandler, false);
ajax.addEventListener("abort", abortHandler, false);
ajax.open("POST", "file_upload_parser.php");
ajax.send(formdata);
}
function progressHandler(event){
//_("loaded_n_total").innerHTML = "Uploaded "+event.loaded+" bytes of "+event.total;
var percent = (event.loaded / event.total) * 100;
_("progressBar").value = Math.round(percent);
_("status").innerHTML = Math.round(percent)+"% uploaded... please wait";
}
function completeHandler(event){
_("status").innerHTML = event.target.responseText;
_("progressBar").value = 0;
document.getElementById('p1').innerHTML = "Drag your file here or click in this area.";
}
function errorHandler(event){
_("status").innerHTML = "Upload Failed";
}
function abortHandler(event){
_("status").innerHTML = "Upload Aborted";
}
function changeText()
{
document.getElementById('p1').innerHTML = "1 file selected";
}
</script>
<link rel="stylesheet" href="test.css">
</head>
<body>
<h2>Upload</h2>
<fieldset>
<legend>Group 1</legend>
<form id="upload_form" enctype="multipart/form-data" method="post">
<input type="file" name="file1" id="file1"><br>
<p id="p1">Drag your file here or click in this area.</p>
<input type="button" value="Upload File" onclick="uploadFile()">
<progress id="progressBar" value="0" max="100" style="width:508px; margin-left: -4px; margin-top: 10px;"></progress>
<h3 id="status"></h3>
<p id="loaded_n_total"></p>
</form>
</fieldset>
<script>
// self executing function here
(function() {
document.getElementById('upload_form')[0].onchange = changeText;
})();
</script>
</body>
</html>
Here is the CSS (which is referred to in the html as test.css):
body{
background: rgba(0,0,0,0.0);
}
form{
position: absolute;
top: 50%;
left: 50%;
margin-top: -100px;
margin-left: -250px;
width: 500px;
height: 200px;
border: 4px dashed #0D0D0D;
}
form p{
width: 100%;
height: 100%;
text-align: center;
line-height: 140px;
color: #0D0D0D;
font-family: Arial;
}
h2{
text-align: center;
}
form input[type="file"]{
position: absolute;
margin: 0;
padding: 0;
width: 100%;
height: 100%;
outline: none;
opacity: 0;
}
form input[type="button"]{
margin: 0;
color: #fff;
background: #16a085;
border: none;
width: 508px;
height: 35px;
margin-top: -20px;
margin-left: -4px;
border-radius: 4px;
border-bottom: 4px solid #117A60;
transition: all .2s ease;
outline: none;
}
form input[type="button"]:hover{
background: #149174;
color: #0C5645;
}
form input[type="button"]:active{
border:0;
}
form progressBar{
text-align: center;
}
Coming back to the HTML, the fieldset tags are placed around the controls that I am attempting to encapsulate. I left them there so that anyone can see the main issue that I am running into.
I apologize but I am very new to web programming. Any help will be greatly appreciated, thank you.
Note: how the box is created doesn't really matter to me. I would expect that the box is created in HTML and then I can style it using CSS.
The structure of your HTML is fine, but the position: absolute properties in your CSS are clashing with the fieldset.
Since <fieldset> is wrapping all your controls, I would suggeset giving it a fixed width and height and position your child elements based on that, i.e. use width: 100% for your children and give all of them the same margin so they align nicely. Also make sure you either edit your #progressBar style in the markup.
Here's a snippet with the changes I just mentioned:
body {
background: rgba(0, 0, 0, 0.0);
}
fieldset {
width: 508px;
height: 270px;
/* fixed width and height*/
margin: 13vh auto;
}
#p1 {
border: 4px dashed #0D0D0D;
/* modified the actual text box instead of the entire form */
width: 508px;
height: 140px;
line-height: 140px;
margin-top: 0px;
}
form p {
text-align: center;
color: #0D0D0D;
font-family: Arial;
}
h2 {
text-align: center;
}
form input[type="file"] {
position: absolute;
margin: 0;
outline: none;
width: 508px;
height: 140px;
margin: 22px 4px;
opacity: 1;
background-color: orange;
/* Last two properties are a visual representation. Delete background-color and set opacity to 0 */
}
form input[type="button"] {
margin: 0;
color: #fff;
background: #16a085;
border: none;
width: 100%;
/* width relative to parent fieldset */
height: 35px;
margin-top: -20px;
border-radius: 4px;
border-bottom: 4px solid #117A60;
transition: all .2s ease;
outline: none;
}
form input[type="button"]:hover {
background: #149174;
color: #0C5645;
}
form input[type="button"]:active {
border: 0;
}
form progressBar {
text-align: center;
}
<!DOCTYPE html>
<html>
<head>
<script>
/* Script written by Adam Khoury # DevelopPHP.com */
/* Video Tutorial: http://www.youtube.com/watch?v=EraNFJiY0Eg */
function _(el) {
return document.getElementById(el);
}
function uploadFile() {
var file = _("file1").files[0];
// alert(file.name+" | "+file.size+" | "+file.type);
var formdata = new FormData();
formdata.append("file1", file);
var ajax = new XMLHttpRequest();
ajax.upload.addEventListener("progress", progressHandler, false);
ajax.addEventListener("load", completeHandler, false);
ajax.addEventListener("error", errorHandler, false);
ajax.addEventListener("abort", abortHandler, false);
ajax.open("POST", "file_upload_parser.php");
ajax.send(formdata);
}
function progressHandler(event) {
//_("loaded_n_total").innerHTML = "Uploaded "+event.loaded+" bytes of "+event.total;
var percent = (event.loaded / event.total) * 100;
_("progressBar").value = Math.round(percent);
_("status").innerHTML = Math.round(percent) + "% uploaded... please wait";
}
function completeHandler(event) {
_("status").innerHTML = event.target.responseText;
_("progressBar").value = 0;
document.getElementById('p1').innerHTML = "Drag your file here or click in this area.";
}
function errorHandler(event) {
_("status").innerHTML = "Upload Failed";
}
function abortHandler(event) {
_("status").innerHTML = "Upload Aborted";
}
function changeText() {
document.getElementById('p1').innerHTML = "1 file selected";
}
</script>
<link rel="stylesheet" href="test.css">
</head>
<body>
<h2>Upload</h2>
<fieldset>
<legend>Group 1</legend>
<form id="upload_form" enctype="multipart/form-data" method="post">
<input type="file" name="file1" id="file1"><br>
<p id="p1">Drag your file here or click in this area.</p>
<input type="button" value="Upload File" onclick="uploadFile()">
<!-- changed progressBar style -->
<progress id="progressBar" value="0" max="100" style="width:100%; margin-top: 10px;"></progress>
<h3 id="status"></h3>
<p id="loaded_n_total"></p>
</form>
</fieldset>
<script>
// self executing function here
(function() {
document.getElementById('upload_form')[0].onchange = changeText;
})();
</script>
</body>
</html>
Hope it helps!
Can I customize
<input type='number'>
field to show all the time it's arrows? By default it's hidden till the field is has no focus. Below is what I'm talking about.
Firefox and IE don't have such behavior. So, I assume you are working with Google Chrome.
input::-webkit-inner-spin-button {
opacity: 1;
}
FYI. UA stylesheet has the following:
input::-webkit-inner-spin-button {
...
opacity: 0;
pointer-events: none;
}
input:enabled:read-write:-webkit-any(:focus,:hover)::-webkit-inner-spin-button {
opacity: 1;
pointer-events: auto;
}
html.css
The UI and behavior of <input type='number'>, as well as all the other HTML5 input types (e.g., type='date', etc), is browser and/or system dependent. To make the arrows always visible, you'd need to use a custom JS solution.
Only way that I can think of is... Having two buttons for incrementing and decrementing your input and using JS. You won't be using type="number" here since the JS will be incrementing and decrementing the number for you.
Here is an example, as mentioned here:
CSS:
.spin {
display: inline-block;
}
.spin span {
display: inline-block;
width: 20px;
height: 22px;
text-align: center;
padding-top: 2px;
background: #fff;
border: 1px solid #aaa;
border-radius: 0 4px 4px 0;
cursor: pointer;
}
.spin span:first-child {
border-radius: 4px 0 0 4px;
}
.spin input {
width: 40px;
height: 20px;
text-align: center;
font-weight: bold;
}
JS:
var spins = document.getElementsByClassName("spin");
for (var i = 0, len = spins.length; i < len; i++) {
var spin = spins[i],
span = spin.getElementsByTagName("span"),
input = spin.getElementsByTagName("input")[0];
input.onchange = function() { input.value = +input.value || 0; };
span[0].onclick = function() { input.value = Math.max(0, input.value - 1); };
span[1].onclick = function() { input.value -= -1; };
}
Note: Change background: #fff; to change the arrow colors. There are other neat examples available on the web as well!
Demo