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


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 相关文章推荐
超级强大的表单验证
Jun 26 Javascript
在IE中调用javascript打开Excel的代码(downmoon原作)
Apr 02 Javascript
javascript仿qq界面的折叠菜单实现代码
Dec 12 Javascript
JavaScript对象创建及继承原理实例解剖
Feb 28 Javascript
简介JavaScript中Math.LOG10E属性的使用
Jun 14 Javascript
使用jQuery的easydrag插件实现可拖动的DIV弹出框
Feb 19 Javascript
JavaScript纯色二维码变成彩色二维码
Jul 23 Javascript
webpack构建的详细流程探底
Jan 08 Javascript
Bootstrap 按钮样式与使用代码详解
Dec 09 Javascript
JavaScript冒泡算法原理与实现方法深入理解
Jun 04 Javascript
vue中keep-alive、activated的探讨和使用详解
Jul 26 Javascript
vue图片裁剪插件vue-cropper使用方法详解
Dec 16 Vue.js
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
新版PHP极大的增强功能和性能
2006/10/09 PHP
php使用number_format函数截取小数的方法分析
2016/05/27 PHP
js getElementsByTagName的简写方式
2010/06/27 Javascript
jquery 判断滚动条到达了底部和顶端的方法
2014/04/02 Javascript
JavaScript定义变量和变量优先级问题探讨
2014/10/11 Javascript
js正则表达式验证邮件地址
2015/11/12 Javascript
javascript bom是什么及bom和dom的区别
2015/11/26 Javascript
jquery.validate使用详解
2016/06/02 Javascript
Javascript类型系统之undefined和null浅析
2016/07/13 Javascript
js时间比较 js计算时间差的简单实现方法
2016/08/26 Javascript
基于Vue2的移动端开发环境搭建详解
2016/11/03 Javascript
Node.js中路径处理模块path详解
2016/11/14 Javascript
jQuery EasyUI的TreeGrid查询功能实现方法
2017/08/08 jQuery
Vue 过滤器filters及基本用法
2017/12/26 Javascript
JavaScript控制浏览器全屏显示简单示例
2018/07/05 Javascript
JS动画实现回调地狱promise的实例代码详解
2018/11/08 Javascript
vue 中 beforeRouteEnter 死循环的问题
2019/04/23 Javascript
Node.js系列之发起get/post请求(2)
2019/08/30 Javascript
Python的Flask框架中实现分页功能的教程
2015/04/20 Python
Python设计模式编程中Adapter适配器模式的使用实例
2016/03/02 Python
浅谈pandas中DataFrame关于显示值省略的解决方法
2018/04/08 Python
使用Python监视指定目录下文件变更的方法
2018/10/15 Python
python实现栅栏加解密 支持密钥加密
2019/03/20 Python
matplotlib 多个图像共用一个colorbar的实现示例
2020/09/10 Python
南非领先的在线旅行社:Travelstart南非
2016/09/04 全球购物
巴西葡萄酒销售网站:Wine.com.br
2017/11/07 全球购物
Audible英国:有声读物,30天免费试用
2019/10/16 全球购物
优秀共产党员先进事迹
2014/01/27 职场文书
邓小平文选读书笔记
2015/06/29 职场文书
幼儿园教师培训心得体会
2016/01/21 职场文书
描述鲁迅的名言整理,一生受用
2019/08/08 职场文书
在Windows下安装配置CPU版的PyTorch的方法
2021/04/02 Python
react如何快速设置文件路径别名
2021/04/28 Javascript
浅谈sql_@SelectProvider及使用注意说明
2021/08/04 Java/Android
Matplotlib绘制条形图的方法你知道吗
2022/03/21 Python
vue使用echarts实现折线图
2022/03/21 Vue.js