一个支持任意尺寸的图片上下左右滑动效果


Posted in Javascript onAugust 24, 2014

常常遇到图片通过后台上传后就变形了的问题,如果你的网站风格适合,可以用这种方式来给页面布局,支持任意尺寸的图片滑动(上下左右滑动)

<! DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>任意尺寸的图片滑动</title>
<style>
div { margin: 0 auto; overflow: hidden;}
.main { width: 1000px;}
.divimg_div1 { width: 380px; float: left;}
.divimg .div4_title { width: 380px; height: 103px; background-color: #EDB205; color: #FFF; font-family: "微软雅黑"; font-size: 22px; font-weight: bold; line-height: 90px; text-align: center; letter-spacing: 5px;}
.divimg_img1 { width: 380px; height: 414px; margin-top: 5px; background-color: #FFF; position: relative;}
.divimg_div2 { width: 615px; float: right;}
.divimg_img2 { width: 194px; height: 256px; float: left; background-color: #FFF; position: relative;}
.divimg_img3 { width: 417px; height: 256px; float: right; background-color: #FFF; position: relative;}
.divimg_img4 { width: 366px; height: 262px; float: left; margin-top: 4px; background-color: #FFF; position: relative;}
.divimg_img5 { width: 245px; height: 262px; float: right; margin-top: 4px; background-color: #FFF; position: relative;}
.divimg .gif { position: absolute; left: 50%; top: 50%; margin-left: -50px; margin-top: -50px; z-index: 2;}
.divimg .img { position: absolute; z-index: 1; left: 0; top: 0; display: none;}
</style>
<script src="js/jquery-1.10.2.min.js"></script>
<script>
$(document).ready(function(e) {
$(".divimg .img").load(function(){
var w=parseInt($(this).width());
var h=parseInt($(this).height());
var hh=$(this).parent().height();
var ww=$(this).parent().width();
var blw=w/parseInt(ww);
var blh=h/parseInt(hh);
function left(){
$(this).animate({left:-(parseInt(parseInt(hh)/h*w)-(parseInt(ww)))},10000,right)
}
function right(){
$(this).animate({left:0},10000,left);
}
function top(){
$(this).animate({top:-(parseInt(parseInt(ww)/w*h)-(parseInt(hh)))},10000,bottom);
}
function bottom(){
$(this).animate({top:0},10000,top);
}
if(blw > blh)
{
$(this).height(hh).width(parseInt(parseInt(hh)/h*w));
$(this).prev().hide(); 
$(this).css({"z-index":"3","display":"block"}); 
$(this).animate({left:-(parseInt(parseInt(hh)/h*w)-(parseInt(ww)))},10000,right);
}
else if(blw < blh)
{
$(this).height(parseInt(parseInt(ww)/w*h)).width(ww);
$(this).prev().hide();
$(this).css({"z-index":"3","display":"block"});
$(this).animate({top:-(parseInt(parseInt(ww)/w*h)-(parseInt(hh)))},10000,bottom);
}
});
$(".div4 .img").each(function(index, element) {
$(this).attr("src",$(this).attr("name"));
});
}); 
</script>
</head>
<body>
<div class="main">
<div class="divimg">
<div class="divimg_div1">
<div class="divimg_title">任意尺寸的图片滑动</div>
<div class="divimg_img1"> <img class="gif" src="images/loadding.gif" width="100" height="100" /> <img class="img" name="images/am_bigimg01.jpg" src="" /> </div>
</div>
<div class="divimg_div2">
<div class="divimg_img2"> <img class="gif" src="images/loadding.gif" width="100" height="100" /> <img class="img" name="images/am_nyimg01.jpg" src="" /> </div>
<div class="divimg_img3"> <img class="gif" src="images/loadding.gif" width="100" height="100" /> <img class="img" name="images/am_img02.jpg" src="" /> </div>
<div class="divimg_img4"> <img class="gif" src="images/loadding.gif" width="100" height="100" /> <img class="img" name="images/am_newimg01.jpg" src="" /> </div>
<div class="divimg_img5"> <img class="gif" src="images/loadding.gif" width="100" height="100" /> <img class="img" name="images/am_rynewimg03.jpg" src="" /> </div>
</div>
</div>
</div>
</body>
</html>
Javascript 相关文章推荐
JavaScript中yield实用简洁实现方式
Jun 12 Javascript
根据出生日期自动取得星座的js代码
Jul 20 Javascript
关于COOKIE个数与大小的问题
Jan 17 Javascript
基于jquery的获取浏览器窗口大小的代码
Mar 28 Javascript
用jquery和json从后台获得数据集的代码
Nov 07 Javascript
仅IE支持clearAttributes/mergeAttributes方法使用介绍
May 04 Javascript
javascript中不易分清的slice,splice和split三个函数
Mar 29 Javascript
jQuery 监控键盘一段时间没输入
Apr 22 Javascript
Node.js+Express配置入门教程
May 19 Javascript
Bootstrap表单制作代码
Mar 17 Javascript
javascript+css3开发打气球小游戏完整代码
Nov 28 Javascript
使用原生javascript开发计算器实例代码
Feb 21 Javascript
jquery 取子节点及当前节点属性值的方法
Aug 24 #Javascript
在JS数组特定索引处指定位置插入元素的技巧
Aug 24 #Javascript
js获取checkbox复选框选中的选项实例
Aug 24 #Javascript
jQuery异步加载数据并添加事件示例
Aug 24 #Javascript
Jquery通过JSON字符串创建JSON对象
Aug 24 #Javascript
Jquery中扩展方法extend使用技巧
Aug 24 #Javascript
jquery使用$(element).is()来判断获取的tagName
Aug 24 #Javascript
You might like
Get或Post提交值的非法数据处理
2006/10/09 PHP
php判断ip黑名单程序代码实例
2014/02/24 PHP
用 Composer构建自己的 PHP 框架之基础准备
2014/10/30 PHP
php无限级评论嵌套实现代码
2018/04/18 PHP
window.parent与window.openner区别介绍
2012/04/12 Javascript
JavaScript实现的简单幂函数实例
2015/04/17 Javascript
在javascript中创建对象的各种模式解析
2016/05/16 Javascript
JavaScript函数中关于valueOf和toString的理解
2016/06/14 Javascript
Angular.js 实现数字转换汉字实例代码
2016/07/14 Javascript
js插件dropload上拉下滑加载数据实例解析
2016/07/27 Javascript
Angular中实现树形结构视图实例代码
2017/05/05 Javascript
JavaScript学习笔记之惰性函数示例详解
2017/08/27 Javascript
Angular2实现组件交互的方法分析
2017/12/19 Javascript
vue 指定组件缓存实例详解
2018/04/01 Javascript
JavaScript对象拷贝与赋值操作实例分析
2018/12/10 Javascript
element el-tree组件的动态加载、新增、更新节点的实现
2020/02/27 Javascript
vue实现下拉菜单树
2020/10/22 Javascript
python切换hosts文件代码示例
2013/12/31 Python
Python中多线程及程序锁浅析
2015/01/21 Python
python网络编程之文件下载实例分析
2015/05/20 Python
Python实现统计给定列表中指定数字出现次数的方法
2018/04/11 Python
python 实现读取一个excel多个sheet表并合并的方法
2019/02/12 Python
Python 获取numpy.array索引值的实例
2019/12/06 Python
详解python命令提示符窗口下如何运行python脚本
2020/09/11 Python
python中append函数用法讲解
2020/12/11 Python
俄罗斯眼镜网: optikaworld
2016/07/31 全球购物
Smallable英国家庭概念店:设计师童装及家居装饰
2017/07/05 全球购物
Spartoo西班牙官网:法国时尚购物网站
2018/03/27 全球购物
台湾演唱会订票网站:StubHub台湾
2019/06/11 全球购物
JSF面试题:如何管量web层中的Bean,用什么标签。如何通过jsp页面与Bean绑定在一起进行处理?
2012/10/05 面试题
工作的心得体会
2013/12/31 职场文书
员工拾金不昧表扬信
2014/01/09 职场文书
霸王洗发水广告词
2014/03/14 职场文书
2016入党培训心得体会范文
2016/01/08 职场文书
介绍信应该怎么开?
2019/04/03 职场文书
详解非极大值抑制算法之Python实现
2021/06/28 Python