内包要素の高さによって可変するモーダル
下からスライドして出てくる感じのモーダル の可変バージョンです。
スライドする動きも、cssからjsのanimateに変わってます。
See the Pen 下からスライドして出てくる可変モーダル by takapen (@takapen) on CodePen.
html
btnコンテンツ
close
btn2コンテンツ2
コンテンツ2
コンテンツ2
コンテンツ2
コンテンツ2
コンテンツ2
コンテンツ2
コンテンツ2close
css
/*モーダル用*/
.js-modal__bg {
width: 100%;
height: 100%;
background-color: rgba(0,0,0,0.6);
position: fixed;
top: 0;
left: 0;
z-index: 10;
display: none;
}
.js-modal__main {
width: calc(100% - 32px);
height: auto;
padding: 16px;
bottom: -81%;
left: 0;
background: #fff;
border-radius:4px 4px 0 0 ;
position: fixed;
z-index: 11;
opacity: 0;
}
.js-modal__btn {
color:#76952f;
cursor: pointer;
-webkit-tap-highlight-color:rgba(0,0,0,0);
}
.js-modal__btn--close {
margin-top: 40px;
text-align: center;
text-decoration: underline;
}
.js-modal__btn--close--fix {
width: 30px;
height: 30px;
//background: #333;
border-radius: 50%;
position: absolute;
top: -50px;
right: 5px;
z-index: 101;
cursor:pointer;
-webkit-tap-highlight-color:rgba(0,0,0,0);
}
.js-modal__btn--close--fix:before{
content:"";
width: 20px;
height: 20px;
border-right:3px solid #fff;
transform: rotate(-45deg);
position: absolute;
top:12px;
left:-3px;
}
.js-modal__btn--close--fix:after{
content:"";
width: 20px;
height: 20px;
border-right:3px solid #fff;
transform: rotate(45deg);
position: absolute;
top:-2px;
left:-3px;
}
jQuery
$(function(){
var modalBtn = $('.js-modal__btn');
var modalBtnClose = $('.js-modal__btn--close');
var modalBtnCloseFix = $('.js-modal__btn--close--fix');
var modalBg = $('.js-modal__bg');
var modalMain = $('.js-modal__main');
modalBtn.on('click', function (e) {
$(this).next(modalBg).fadeIn();
$(this).next(modalBg).next(modalMain).animate({
'bottom': '0',
'opacity': '1'
}, 400);
});
modalBtnClose.on('click', function (e) {
modalBg.fadeOut();
modalMain.animate({
'bottom': '-80%',
'opacity': '0'
}, 400);
});
modalBtnCloseFix.on('click', function (e) {
modalBg.fadeOut();
modalMain.animate({
'bottom': '-80%',
'opacity': '0'
}, 400);
});
modalMain.on('click', function (e) {
e.stopPropagation();
});
modalBg.on('click', function () {
$(this).fadeOut();
$(this).next(modalMain).animate({
'bottom': '-80%',
'opacity': '0'
}, 400);
});
});