目錄結構如下:
常用的jquery方法放入utils.js中
方法索引:
1.水平垂直居中
不受滾動條影響,始終居中
util中的方法:
// 方法掛載到JQ
// 方法作用:居中
jQuery.fn.center = function () {
this.css('position','absolute');
this.css('top', ( $(window).height() - this.height() ) / 2 + $(window).scrollTop() + 'px');
this.css('left', ( $(window).width() - this.width() ) / 2 + $(window).scrollLeft() + 'px');
return this;
}
html:
<div class="center"></div>
css:
.center {
height: 300px;
width: 600px;
background-color: #eee;
}
js:
$(function () {
$('.center').center();
})
2.判斷元素是否可見
js:
if($("div").is(':visible') == true) {
alert("div可見")
}
3.正則表達式 替換文本
js:
$(function () {
var exp = /cxk/;
$(“div”).html(
$(this).text().replace(exp,"lzx")
);
};