第一次写插件,基于jQuery的
2011-07-26 17:11
190 查看
<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
#content{
width:960px; margin:0 auto; height:30px; border:solid 1px Red; background:red; position:relative;
}
#floatTest{
background:Yellow;
border:solid 1px Red;
height:50px;
right:10px;
position:absolute;
top:30px;
width:200px;
z-index:100;
}
</style>
<script src="http://zxj2634.blog.163.com/blog/Scripts/jq.min.js" type="text/javascript"></script>
<script type="text/javascript">
/**
* 要求,有个父元素 position 为 relative
* 参数 distanceTop 距离顶端距离(为空则默认为0)
* @example
* $("#floatTest").float("20px");
*/
jQuery.fn.floatTest = function (distanceTop) {
var $this = $(this); //这里的this是指当前的jQuery对象
var initTop = $this.css("top");
var lengthTop;
//先取得距离顶部的高度lenghtTop = 父元素的高度 + 父元素距离顶部的高度
var getParent = $this.parent();
if (typeof distanceTop === "undefined") {
lengthTop = getParent.attr("offsetTop") + getParent.height();
} else {
var space = parseInt(distanceTop);
lengthTop = getParent.attr("offsetTop") + getParent.height() - space;
}
//页面滚动
$(window).scroll(function () {
var changeTop = $(window).scrollTop();
//滚动的高度小于初始高度
if (changeTop < lengthTop) {
$this.animate({ top: initTop }, 10);
} else { //滚动的高度小于初始高度
var toTop = (parseInt(initTop) + changeTop - lengthTop) + "px";
$this.animate({ top: toTop }, 10);
}
});
}
$(function () {
$("#floatTest").floatTest("20px");
})
</script>
</head>
<body style="margin:0">
<div style="height:200px;"></div>
<div id="content">
<div id="floatTest" style="">aaaa</div>
</div>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
</body>
</html>
<html>
<head>
<title></title>
<style>
#content{
width:960px; margin:0 auto; height:30px; border:solid 1px Red; background:red; position:relative;
}
#floatTest{
background:Yellow;
border:solid 1px Red;
height:50px;
right:10px;
position:absolute;
top:30px;
width:200px;
z-index:100;
}
</style>
<script src="http://zxj2634.blog.163.com/blog/Scripts/jq.min.js" type="text/javascript"></script>
<script type="text/javascript">
/**
* 要求,有个父元素 position 为 relative
* 参数 distanceTop 距离顶端距离(为空则默认为0)
* @example
* $("#floatTest").float("20px");
*/
jQuery.fn.floatTest = function (distanceTop) {
var $this = $(this); //这里的this是指当前的jQuery对象
var initTop = $this.css("top");
var lengthTop;
//先取得距离顶部的高度lenghtTop = 父元素的高度 + 父元素距离顶部的高度
var getParent = $this.parent();
if (typeof distanceTop === "undefined") {
lengthTop = getParent.attr("offsetTop") + getParent.height();
} else {
var space = parseInt(distanceTop);
lengthTop = getParent.attr("offsetTop") + getParent.height() - space;
}
//页面滚动
$(window).scroll(function () {
var changeTop = $(window).scrollTop();
//滚动的高度小于初始高度
if (changeTop < lengthTop) {
$this.animate({ top: initTop }, 10);
} else { //滚动的高度小于初始高度
var toTop = (parseInt(initTop) + changeTop - lengthTop) + "px";
$this.animate({ top: toTop }, 10);
}
});
}
$(function () {
$("#floatTest").floatTest("20px");
})
</script>
</head>
<body style="margin:0">
<div style="height:200px;"></div>
<div id="content">
<div id="floatTest" style="">aaaa</div>
</div>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
</body>
</html>
相关文章推荐
- 第一次写插件,基于jQuery的(2)
- 基于Bootstrap的jQuery slider插件的使用bootstrap-slider.js
- 基于jquery的轮播图插件
- 发一个基于jQuery的多标签浏览插件
- 基于jQuery淡入淡出可自动切换的幻灯插件(2011.01.11更新)
- 基于bootstrap的jQuery多级列表树插件 treeview
- 10款web前端基于jquery超实用jQuery插件大合集
- 分享一些基于jquery功能强大的LightBox灯箱效果插件
- 推荐一款基于 Bootstrap 的 jQuery 表格插件Bootstrap table
- 基于jQuery的时间轴鼠标悬停动画插件
- qTip 基于JQuery的Tooltip插件[兼容性好]
- 10 个基于jQuery的表单增强脚本和插件
- 基于jquery 的插件,让IE支持placeholder属性
- 基于jquery的bootstrap在线文本编辑器插件Summernote
- jQuery相当于对 javascript二次开发,所以基于 jQuery实现的各种插件直接调用即可
- 分享几款基于bootstrap和jquery的分页插件
- jQuery-插件-图片切换(第一次自已偿试写插件)
- 基于jQuery插件之初探
- 自定义基于 jQuery 的 tableUI 插件(基偶行颜色交替,活动行变色)