セレクトボタンの選択し(ドロップダウンメニュー)のも時間を広げてほしいという要望があり、jQueryとcssで実装した。
WordPressを使ったサイトで、お問い合わせフォームには「contact form 7」を使用。ネットで公開されていたソースの一部をcontatform7で動くように書き換えたので、備忘録として残しておきます。
jQuery
contact form 7以外ではselectにクラス名「formCmnSelect」を記述すればOKです。
<script>
$(function() {
$(".wpcf7-select,.formCmnSelect").each(function() {
var classes = $(this).attr("class"),
id = $(this).attr("id"),
name = $(this).attr("name");
var template = '<div class="' + classes + '">';
template += '<span class="custom-select-trigger"> '/* + $(this).attr("placeholder")*/ + '</span>';
template += '<div class="custom-options">';
$(this).find("option").each(function() {
template += '<span class="custom-option ' + $(this).attr("class") + '" data-value="' + $(this).attr("value") + '">' + $(this).html() + '</span>';
});
template += '</div></div>';
$(this).wrap('<div class="custom-select-wrapper"></div>');
$(this).hide();
$(this).after(template);
});
$(".custom-option:first-of-type").hover(function() {
$(this).parents(".custom-options").addClass("option-hover");
}, function() {
$(this).parents(".custom-options").removeClass("option-hover");
});
$(".custom-select-trigger").on("click", function() {
$('html').one('click',function() {
$(".wpcf7-select,.formCmnSelect").removeClass("opened");
});
$(this).parents(".wpcf7-select,.formCmnSelect").toggleClass("opened");
event.stopPropagation();
});
$(".custom-option").on("click", function() {
$(this).parents(".custom-select-wrapper").find("select").val($(this).data("value"));
$(this).parents(".custom-options").find(".custom-option").removeClass("selection");
$(this).addClass("selection");
$(this).parents(".wpcf7-select,.formCmnSelect").removeClass("opened");
$(this).parents(".wpcf7-select,.formCmnSelect").find(".custom-select-trigger").text($(this).text());
});
});
CSS
contact form 7以外ではselectにクラス名「formCmnSelect」を記述すればOKです。
/** Custom Select **/
.custom-select-wrapper {
position: relative;
display: inline-block;
user-select: none;
}
.custom-select-wrapper select {
display: none;
}
.wpcf7-select,
.formCmnSelect{
position: relative;
display: inline-block;
}
.custom-select-trigger {
position: relative;
display: block;
width: 240px;
padding: 0 30px 0 13px;
border: 1px solid #BABABA;
background: #fff;
font-size: 15px;
font-weight: 300;
line-height: 42px;
cursor: pointer;
}
.custom-select-trigger:after {
position: absolute;
top: 50%; right: 14px;
display: inline-block;
content: "";
border-style: solid;
border-color: #6E6E6E transparent transparent transparent;
border-width: 12px 6px 6px;
width: 0;
height: 0;
margin-top: 2px;
transform: translateY(-50%);
}
.wpcf7-select.opened .custom-select-trigger:after,
.formCmnSelect.opened .custom-select-trigger:after {
margin-top: -12px;
transform: rotate(-180deg);
}
.custom-options {
position: absolute;
display: block;
top: 100%; left: 0; right: 0;
min-width: 100%;
border: 1px solid #BABABA;
border-radius: 0;
box-sizing: border-box;
box-shadow: 0 2px 1px rgba(0,0,0,.07);
background: #fff;
transition: all .4s ease-in-out;
opacity: 0;
visibility: hidden;
pointer-events: none;
transform: translateY(-15px);
z-index: 2;
overflow-y: scroll;
max-height: 360px;
}
.wpcf7-select.opened .custom-options,
.formCmnSelect.opened .custom-options{
opacity: 1;
visibility: visible;
pointer-events: all;
transform: translateY(0);
}
.custom-option {
position: relative;
display: block;
padding: 0 10px 0 13px;
font-size: 14px;
line-height: 38px;
cursor: pointer;
transition: all .4s ease-in-out;
}
.custom-option:first-of-type {
border-radius: 0;
}
.custom-option:last-of-type {
border-bottom: 0;
border-radius: 0px;
}
.custom-option:hover,
.custom-option.selection {
background: #f9f9f9;
}