一个简单的九宫画册效果
2009-04-08 18:05
330 查看
以前看到这种九宫格一样的图片效果,很羡慕。心血来潮,就模仿写了下。
越写越冒汗,觉得实在是很简单(当然要写完善也不简单),越来越觉得自己JS水平实在不怎么样。看看代码,才短短几行,唉,现在觉得能写出jQuery 插件的人才能算是有水平的人了。
代码如下:
HTML:
<script type="text/javascript">
var edge = { width: $('ul').width(), height: $('ul').height() };
var SideWidth = 100;
$(function() {
//待所有LI自动就位后,设置好position这个CSS属性
$('ul>li').each(function() {
$(this).css({ "left": $(this).position().left + "px", "top": $(this).position().top + "px" });
$(this).attr('ref', $(this).css('left')).attr('lang', $(this).css('top')); //记录原始left和top,用于恢复位置
}).each(function() {
$(this).css({ "position": "absolute" });
});
$('ul>li').hover(function() {
$(this).css({ "z-index": "10" }).animate({ width: SideWidth + "px", height: SideWidth + "px", left: fnGetRelateLength(this, "left"), top: fnGetRelateLength(this, "top") }, "fast");
}, function() {
$(this).animate({ width: "58px", height: "58px", "top": $(this).attr('lang'), 'left': $(this).attr('ref') }, "fast", function() { $(this).css({ "z-index": "0" }) });
});
});
//获取相对的绝对定位位置
function fnGetRelateLength(obj, attr) {
var theObj = $(obj);
var relateLength = null;
if (parseInt(theObj.css(attr).replace("px", "")) > 0) {//防止越界
relateLength = theObj.css(attr).replace("px", "") - 20;
if (relateLength + SideWidth > (attr == "top" ? edge.height : edge.width)) {
relateLength = (attr == "top" ? edge.height : edge.width) - SideWidth - 2;//-2是为了让它的border有显示。这个位置不好,要处理
}
} else {
relateLength = 0;
}
theObj = null;
return relateLength + "px";
}
</script>
越写越冒汗,觉得实在是很简单(当然要写完善也不简单),越来越觉得自己JS水平实在不怎么样。看看代码,才短短几行,唉,现在觉得能写出jQuery 插件的人才能算是有水平的人了。
代码如下:
HTML:
<script type="text/javascript">
var edge = { width: $('ul').width(), height: $('ul').height() };
var SideWidth = 100;
$(function() {
//待所有LI自动就位后,设置好position这个CSS属性
$('ul>li').each(function() {
$(this).css({ "left": $(this).position().left + "px", "top": $(this).position().top + "px" });
$(this).attr('ref', $(this).css('left')).attr('lang', $(this).css('top')); //记录原始left和top,用于恢复位置
}).each(function() {
$(this).css({ "position": "absolute" });
});
$('ul>li').hover(function() {
$(this).css({ "z-index": "10" }).animate({ width: SideWidth + "px", height: SideWidth + "px", left: fnGetRelateLength(this, "left"), top: fnGetRelateLength(this, "top") }, "fast");
}, function() {
$(this).animate({ width: "58px", height: "58px", "top": $(this).attr('lang'), 'left': $(this).attr('ref') }, "fast", function() { $(this).css({ "z-index": "0" }) });
});
});
//获取相对的绝对定位位置
function fnGetRelateLength(obj, attr) {
var theObj = $(obj);
var relateLength = null;
if (parseInt(theObj.css(attr).replace("px", "")) > 0) {//防止越界
relateLength = theObj.css(attr).replace("px", "") - 20;
if (relateLength + SideWidth > (attr == "top" ? edge.height : edge.width)) {
relateLength = (attr == "top" ? edge.height : edge.width) - SideWidth - 2;//-2是为了让它的border有显示。这个位置不好,要处理
}
} else {
relateLength = 0;
}
theObj = null;
return relateLength + "px";
}
</script>
相关文章推荐
- Javascript实现一个简单的输入关键字添加标签效果实例
- jquery 一个简单筛选 显示隐藏效果
- Unity3d 一个简单的玻璃效果实现!
- 请写一个简单的幻灯效果页面。如果不使用JS来完成,可以加分。
- 请写一个简单的幻灯效果页面。如果不使用JS来完成,可以加分。
- 一个简单的Viewport效果
- 一个小型的网站,比如个人网站,可以使用最简单的html静态页面就实现了,配合一些图片达到美化效果,所有的页面均存放在一个目录下,这样的网站对系统架构、性能的要求都很简单,随着互联网业务的不断丰富,网站
- 一个简单又漂亮的JS弹出窗体效果
- Bootboxjs是一个简单的js库,简单快捷帮你制作一个Bootstrap的弹出框效果。
- 实现一个菜单的简单的动画效果
- 一个简单的行拖动效果
- Android开发——使用BaseAdapter做一个简单的新闻效果
- 哪种缓存效果高?开源一个简单的缓存组件j2cache
- 一个简单带动画效果tableview弹窗菜单
- 请写一个简单的幻灯效果页面,不使用JS来完成怎么做
- 一个简单的瀑布流效果(主体形式自写)
- js实现一个简单的数字时钟效果
- 一个简单效果用到的js特性(异步编程,匿名函数,闭包)
- 代码录播:jQueryMobile 实现一个简单的弹出框效果