Required JavaScript:
$(document).on('click', '.year-select .select-years-container .pick-year.selectable', function(e){
var year = parseInt($(this).text()),
ele = $('[year-select-identifier-input="' + $(this).attr('year-select-identifier') + '"]')
ele.val(year)
$('body').find('.year-select').remove()
})
$(document).on('click', '.year-select a.year-close', function(e){
e.preventDefault()
$('body').find('.year-select').remove()
})
function showYear(ele,options){
$('body').find('.year-select').remove()
var date = new Date(),
nowYear = parseInt(date.getFullYear()),
year = (parseInt(ele.val()) > 0 ? parseInt(ele.val()) : parseInt(date.getFullYear())),
years = [],
yearLeft = ele.offset().left,
yearTop = ele.offset().top + ele.outerHeight() + 5,
html = '',
before_now = options.years_before_now ? options.years_before_now : 30,
after_now = (options.years_after_now ? options.years_after_now : 10) + 1,
identifier = Math.floor(Math.random() * 10000000)
ele.attr('year-select-identifier-input', identifier)
while(before_now > 0){
years.push(nowYear - before_now)
before_now --
}
after_now_loop = (after_now)
while(after_now_loop > 0){
years.push(nowYear + Math.abs(after_now_loop - after_now))
after_now_loop --
}
var html = '<div class="year-select" style="left:' + yearLeft + 'px;top:' + yearTop +'px">'
html += '<div class="selected-year">' + year + '<a class="year-close">x</a></div>'
html += '<div class="select-years-container">'
years.forEach(function(value){
var theClass = (year == value ? 'active' : 'selectable')
html += '<div class="pick-year ' + theClass + '" year-select-identifier="' + identifier + '">' + value +'</div>'
})
html += '</div>'
html += '</div>'
$('body').append(html)
$('.year-select .select-years-container').animate({
scrollTop: ($('.year-select .pick-year.active').offset().top - yearTop - $('.year-select .selected-year').outerHeight() - $('.year-select .pick-year.active').outerHeight())
},0)
}
Required CSS:
.year-select{
position: absolute;
background: white;
width: 100%;
max-width: 320px;
box-shadow: 0 0 6px #cdcdcd;
border-radius:6px;
}
.year-select a.year-close{
position: absolute;
right: 0;
top: 0;
background: #f7d7da;
width: 30px;
height: 30px;
border-radius: 0 6px 0 6px;
font-size: 15px;
color: #721c24;
line-height: 30px;
}
.year-select a.year-close:hover{
background: #721c24;
color:white;
}
.year-select .selected-year{
padding: 15px 20px;
border-bottom: 1px solid #cdcdcd;
text-align: center;
font-size:20px;
}
.year-select .select-years-container{
max-height: 240px;
overflow: auto;
}
.year-select .pick-year {
float: left;
width: 33.333%;
padding: 10px;
box-sizing: border-box;
text-align: center;
color:#333;
}
.year-select .pick-year.active,.year-select .pick-year:hover{
background: #ebebeb;
color:#999;
cursor: pointer
}
Example code to activate script:
$('input[type="year"]').click(function(e){
e.preventDefault()
showYear($(this),{})
})
$('input[type="year"]').keydown(function(e){
e.preventDefault()
})
Default options:
{
years_before_now: 30,
years_after_now: 10
}
Years before now (years_before_now):
This is the amount of years before current year, for example if you set it on 5 the years will be [2015, 2016, 2017, 2018, 2019, 2020]
Years after now (years_after_now):
This is the amount of years after the current year, for example if you set it on 5 the years will be [2020, 2021, 2022, 2023, 2024, 2025]