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


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 相关文章推荐
kmock javascript 单元测试代码
Feb 06 Javascript
基于jQuery实现的当离开页面时出现提示的实现代码
Jun 27 Javascript
javascript 基础篇3 类,回调函数,内置对象,事件处理
Mar 14 Javascript
javascript window.confirm确认 取消对话框实现代码小结
Oct 21 Javascript
动态创建script在IE中缓存js文件时导致编码的解决方法
May 04 Javascript
浅谈javascript面向对象程序设计
Jan 21 Javascript
easyui combogrid实现本地模糊搜索过滤多列
May 13 Javascript
vue mixins组件复用的几种方式(小结)
Sep 06 Javascript
浅谈mvvm-simple双向绑定简单实现
Apr 18 Javascript
利用vscode调试编译后的js代码详解
May 14 Javascript
AngularJS实现的base64编码与解码功能示例
May 17 Javascript
JavaScript代码实现简单计算器
Dec 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来处理多个提交任务
2008/05/08 PHP
PHP 命令行参数详解及应用
2011/05/18 PHP
PHP遍历某个目录下的所有文件和子文件夹的实现代码
2013/06/28 PHP
javscript对象原型的一些看法
2010/09/19 Javascript
javascript 禁用IE工具栏,导航栏等等实现代码
2013/04/01 Javascript
Jquery 例外被抛出且未被接住原因介绍
2013/09/04 Javascript
JavaScript基于ajax编辑信息用法实例
2015/07/15 Javascript
bootstrap中使用google prettify让代码高亮的方法
2016/10/21 Javascript
jQuery制作图片旋转效果
2017/02/02 Javascript
Vue.js tab实现选项卡切换
2017/05/16 Javascript
利用jquery去掉时光轴头尾部线条的方法实例
2017/06/16 jQuery
React Native验证码倒计时工具类分享
2017/10/24 Javascript
Angular中支持SCSS的方法
2017/11/18 Javascript
js实现手机web图片左右滑动效果
2017/12/29 Javascript
小程序测试后台服务的方法(ngrok)
2019/03/08 Javascript
vue远程加载sfc组件思路详解
2019/12/25 Javascript
JQuery事件冒泡和默认行为代码实例
2020/05/13 jQuery
[01:16]DOTA2小知识课堂 Ep.03 芒果树无伤肉山
2019/12/05 DOTA
解读Python编程中的命名空间与作用域
2015/10/16 Python
Python实现的密码强度检测器示例
2017/08/23 Python
Python使用 Beanstalkd 做异步任务处理的方法
2018/04/24 Python
如何用Python合并lmdb文件
2018/07/02 Python
selenium+python 对输入框的输入处理方法
2018/10/11 Python
pandas的连接函数concat()函数的具体使用方法
2019/07/09 Python
8段用于数据清洗Python代码(小结)
2019/10/31 Python
python requests库的使用
2021/01/06 Python
巧用CSS3 border实现图片遮罩效果代码
2012/04/09 HTML / CSS
新加坡最佳婴儿用品店:Mamahood.com.sg
2018/08/26 全球购物
欧洲最大的预定车位市场:JustPark
2020/01/06 全球购物
汽车销售员如何做职业生涯规划
2014/02/16 职场文书
和睦家庭事迹
2014/05/14 职场文书
我们的节日中秋活动方案
2014/08/19 职场文书
写给老师的感谢信
2015/01/20 职场文书
大雁塔导游词
2015/02/04 职场文书
干部培训简讯
2015/07/20 职场文书
MySQL中JOIN连接的基本用法实例
2022/06/05 MySQL