(function ($) {
/**
*设置元素的fixed或者static的触发
*
* @author ******@tuniu.com
* @private sticky
* @param {Object} options
*/
$.fn.sticky = function (options) {
var isFixed = false; //是否是固定位置
var self = this;
var defaults = {
y: 0 // 元素固定位置时距顶部的距离
};
this.settings = $.extend(defaults, options);
this.offsetY = $(this).offset().top - this.settings.y; //元素固定距离状态触发时,body的scrollTop()
$(window).scroll(function () {
var scrollTop = $(window).scrollTop();
if (!isFixed && scrollTop > self.offsetY) {
$(self).css({
position: 'fixed',
left: 0,
top: self.settings.y
});
isFixed = true;
} else if (isFixed && scrollTop <= self.offsetY) {
$(self).css({
position: 'static'
});
isFixed = false;
}
});
};
/**
*a标签点击后到达另外一个元素的位置的动画
*
* @author ******@tuniu.com
* @private {Method} positionScroll
* @param {Object} options //到达另外一个位置时距顶部的距离
*/
$.fn.positionScroll = function (options) {
var defaults = { flag: 0 };
var items = $(this).find('a');
var opt = $.extend(defaults, options);
items.bind('click', function (e) {
if (!$(this).data('to')) return false;
$('html,body').animate({
scrollTop: $($(this).data('to')).offset()
.top - opt.flag
}, 500);
e.preventDefault();
});
};
/**
* 弹出对话框
*
* @param option {Object} 弹出的对话框的设置
*
*/
$.dialog = function(option) {
var defaults = {
id: 'dialog' + (+new Date()),
title: '',
content: '',
showClose: true,
opacity: 60,
zIndex: 9999
};
var closeHtml = '';
var element = null;
var bodyHeight = $('body').height();
var settings = $.extend(defaults, option);
function setLayer() {
var scrollTop = $(window).scrollTop();
var scrollLeft = $(window).scrollLeft();
var gutterW = $(window).width() - element.outerWidth();
var gutterH = $(window).height() - element.outerHeight();
if(gutterH <= 0) {
gutterH = 30;
}
element.css({
left: gutterW / 2 + scrollLeft,
top : gutterH / 2 + scrollTop
});
};
settings.showClose && (closeHtml += '<a href="javascript:void(0);" class="closeDialog">脳</a>');
$('body').append(
'<div class="dialog-window" id="'+ settings.id +'" style="z-index: '+ (settings.zIndex + 1) +';">'
+'<div class="dialog-header"><h3>'+ settings.title +'</h3>'+ closeHtml +'</div>'
+'<div class="dialog-content">'+ settings.content +'</div>'
+'</div>'
+'<div class="mask" style="height: '+ bodyHeight +'px; z-index: '+ settings.zIndex +'; opacity: '+ (settings.opacity / 100) +'; filter: alpha(opacity='+ settings.opacity +');"></div>'
);
element = $('#' + settings.id);
setLayer();
$(window).on('resize', setLayer);
element.find('.closeDialog').on('click', function() {
element.next('.mask').remove();
element.remove();
});
};
})(jQuery);
.mask {
position: absolute;
top: 0;
left: 0;
width: 100%;
background-color: #000
}
.dialog-window {
position: absolute;
top: 50%;
left: 50%;
width: 648px;
border: 1px solid #3f0f9a;
border-radius: 10px;
background: #fff
}
.dialog-window .dialog-header {
position: relative;
height: 40px
}
.dialog-window .dialog-header h3 {
position: absolute;
top: 18px;
left: -9px;
width: 118px;
border-radius: 15px;
background: #ec374f;
color: #fff;
text-align: center;
font-weight: 600;
font-size: 18px;
line-height: 30px
}
.dialog-window .dialog-content {
padding: 20px;
line-height: 20px
}
.dialog-window .dialog-content p {
margin: 0 0 5px
}
.closeDialog {
position: absolute;
top: -15px;
right: -15px;
width: 30px;
height: 30px;
border-radius: 100%;
background: #232323;
color: #fff;
text-align: center;
font-weight: 600;
font-size: 18px;
font-family: sisum;
line-height: 30px;
transition: background-color .5s
}
.closeDialog:hover {
background-color: #111;
color: #fff
}