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


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 相关文章推荐
js 数组的for循环到底应该怎么写?
May 31 Javascript
javascript实现随机显示星星特效
Jan 28 Javascript
关于Jquery中的事件绑定总结
Oct 26 Javascript
angular分页指令操作
Jan 09 Javascript
JavaScript字符串对象
Jan 14 Javascript
JavaScript控制输入框中只能输入中文、数字和英文的方法【基于正则实现】
Mar 03 Javascript
结合Vue控制字符和字节的显示个数的示例
May 17 Javascript
实例分析编写vue组件方法
Feb 12 Javascript
详解vue 命名视图
Aug 14 Javascript
微信内置浏览器图片查看器的代码实例
Oct 08 Javascript
vue 导航内容设置选中状态样式的例子
Nov 01 Javascript
如何使JavaScript休眠或等待
Apr 27 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
PHP聊天室技术
2006/10/09 PHP
php中通过curl smtp发送邮件
2012/06/05 PHP
探讨PHP JSON中文乱码的解决方法详解
2013/06/06 PHP
分享PHP函数实现数字与文字分页代码
2015/07/28 PHP
解决出现SoapFault (looks like we got no XML document)的问题
2017/06/24 PHP
PHP观察者模式原理与简单实现方法示例
2017/08/25 PHP
ie focus bug 解决方法
2009/09/03 Javascript
Jquery cookie操作代码
2010/03/14 Javascript
ff chrome和ie下全局动态定位的异同及全局高度的取法
2014/06/30 Javascript
js实现完全自定义可带多级目录的网页鼠标右键菜单方法
2015/02/28 Javascript
js检测用户输入密码强度
2015/10/22 Javascript
微信小程序 密码输入(源码下载)
2017/06/27 Javascript
JavaScript取得gridview中获取checkbox选中的值
2017/07/24 Javascript
前端图片懒加载(lazyload)的实现方法(提高用户体验)
2017/08/21 Javascript
Vue中使用 setTimeout() setInterval()函数的问题
2018/09/13 Javascript
在Vue项目中使用Typescript的实现
2019/12/19 Javascript
Vue清除定时器setInterval优化方案分享
2020/07/21 Javascript
解决vue addRoutes不生效问题
2020/08/04 Javascript
Vue实现鼠标经过文字显示悬浮框效果的示例代码
2020/10/14 Javascript
vue data有值,但是页面{{}} 取不到值的解决
2020/11/09 Javascript
Vue+penlayers实现多边形绘制及展示
2020/12/24 Vue.js
python读取json文件并将数据插入到mongodb的方法
2015/03/23 Python
wxPython定时器wx.Timer简单应用实例
2015/06/03 Python
python基础入门学习笔记(Python环境搭建)
2016/01/13 Python
浅谈Pandas中map, applymap and apply的区别
2018/04/10 Python
在pandas多重索引multiIndex中选定指定索引的行方法
2018/11/16 Python
Python3利用Dlib实现摄像头实时人脸检测和平铺显示示例
2019/02/21 Python
Java方面的关于数组和继承的笔面试题
2015/09/18 面试题
自荐书封面下载
2013/11/29 职场文书
团购业务员岗位职责
2014/03/15 职场文书
教师节倡议书
2014/08/30 职场文书
电工实训心得体会
2016/01/14 职场文书
Python数据分析入门之教你怎么搭建环境
2021/05/13 Python
python源码剖析之PyObject详解
2021/05/18 Python
详解使用 CSS prefers-* 规范提升网站的可访问性与健壮性
2021/05/25 HTML / CSS
JavaScript组合继承详解
2021/11/07 Javascript