原创jQuery插件之图片自适应
效果图例如以下:
功能:使图片自适应居中位于容器内
限制:容器须要给定大小
用法:
1、引入jQuery。然后引入fitimg插件
2、给须要图片自适应的容器固定宽高
3、header .account .img { width: 40px; height: 40px; margin: 5px 5px; float: left; }4、加入data-src属性
<div class="img" data-src ="/Images/avatar.jpg"></div>这里并没有写img标签,插件会自己主动生成img,把容器当成你想要呈现的图片就能够了
5、调用
$(".img").fitimg('/Images/捕获.png')括号内为假设data-src指向的图片载入失败的替补图片,假设该图片也载入失败,则该容器会清空容器内全部内容
源码:
(function ($)
{
$.fn.extend({
fitimg: function (errorimg)
{
$(this).each(function ()
{
if ($(this).data('src'))
{
$(this).empty()
var img = document.createElement('img')
$(this).append($(img))
$(img).load(function ()
{
var parent = $(this).parent()
var pWidth = parent.width()
var pHeight = parent.height()
var oWidth = $(this).width()
var oHeight = $(this).height()
if (oWidth / pWidth > oHeight / pHeight)
{
$(this).height(pHeight)
var nWidth = pHeight * oWidth / oHeight
$(this).width(nWidth)
$(this).css('margin-left', -(nWidth - pWidth) / 2)
}
else
{
$(this).width(pWidth)
var nHeight = pWidth * oHeight / oWidth
$(this).height(nHeight)
$(this).css('margin-top', -(nHeight - pHeight) / 2)
}
parent.css('overflow', 'hidden')
}).error(function ()
{
if (errorimg)
{
$(this).parent().data('src', errorimg).fitimg()
}
else
{
$(this).parent().empty()
}
})
$(img).attr('src', $(this).data('src'))
}
})
return $(this)
}
})
})(jQuery)
近期(20150831)又加了两个新的功能
1、等图片载入完毕才显示出来,以免因网络问题导致图片刚開始非常大,然后再由js缩放到恰当大小。这个过程不应让用户看见,所以做了一点小小的处理
2、加入图片自适应选项。曾经仅仅同意拉伸到和容器一样大,如今添加可选參数能够缩小到被容器包裹
新增的參数名叫iszoomin,默觉得放大,也就是说假设不传这个值表示进行放大操作
两种效果对照图例如以下
下面为插件最新的代码
(function ($)
{
$.fn.extend({
fitimg: function (errorimg, iszoomin)
{
$(this).each(function ()
{
$(this).empty()
var img = document.createElement('img')
$(this).append($(img))
img.style.display = 'none'
$(img).load(function ()
{
var parent = $(this).parent()
var pWidth = parent.width()
var pHeight = parent.height()
var oWidth = $(this).width()
var oHeight = $(this).height()
if (oWidth / pWidth > oHeight / pHeight)
{
if (!iszoomin)
{
$(this).height(pHeight)
var nWidth = pHeight * oWidth / oHeight
$(this).width(nWidth)
$(this).css('margin-left', -(nWidth - pWidth) / 2)
}
else
{
$(this).width(pWidth)
var nHeight = pWidth * oHeight / oWidth
$(this).height(nHeight)
$(this).css('margin-top', (pHeight - nHeight) / 2)
}
}
else
{
if (!iszoomin)
{
$(this).width(pWidth)
var nHeight = pWidth * oHeight / oWidth
$(this).height(nHeight)
$(this).css('margin-top', -(nHeight - pHeight) / 2)
}
else
{
$(this).height(pHeight)
var nWidth = pHeight * oWidth / oHeight
$(this).width(nWidth)
$(this).css('margin-left', (pWidth - nWidth) / 2)
}
}
parent.css('overflow', 'hidden')
img.style.display = ''
}).error(function ()
{
if (errorimg)
{
$(this).parent().data('src', errorimg).fitimg(null, iszoomin)
}
else
{
$(this).parent().empty()
}
})
$(img).attr('src', $(this).data('src'))
})
return $(this)
}
})
})(jQuery)
2016/12/11更新
jQuery3.1已经公布,为了适配jQuery3.1,代码改动例如以下
(function ($) {
$.fn.extend({
fitimg: function (errorimg, iszoomin) {
iszoomin = typeof iszoomin === 'undefined' ? false : iszoomin
$(this).each(function () {
$(this).empty()
var img = document.createElement('img')
$(this).append($(img))
img.style.display = 'none'
$(img).on('load', function () {
var parent = $(this).parent()
var pWidth = parent.width()
var pHeight = parent.height()
var oWidth = $(this).width()
var oHeight = $(this).height()
if (oWidth / pWidth > oHeight / pHeight) {
if (!iszoomin) {
$(this).height(pHeight)
var nWidth = pHeight * oWidth / oHeight
$(this).width(nWidth)
$(this).css('margin-left', -(nWidth - pWidth) / 2)
}
else {
$(this).width(pWidth)
var nHeight = pWidth * oHeight / oWidth
$(this).height(nHeight)
$(this).css('margin-top', (pHeight - nHeight) / 2)
}
}
else {
if (!iszoomin) {
$(this).width(pWidth)
var nHeight = pWidth * oHeight / oWidth
$(this).height(nHeight)
$(this).css('margin-top', -(nHeight - pHeight) / 2)
}
else {
$(this).height(pHeight)
var nWidth = pHeight * oWidth / oHeight
$(this).width(nWidth)
$(this).css('margin-left', (pWidth - nWidth) / 2)
}
}
parent.css('overflow', 'hidden')
img.style.display = ''
}).on('error', function () {
if (errorimg) {
$(this).parent().data('src', errorimg).fitimg(null, iszoomin)
}
else {
$(this).parent().empty()
}
})
$(img).attr('src', $(this).data('src'))
})
return $(this)
}
})
})(jQuery)
原创jQuery插件之图片自适应的更多相关文章
- 原创jquery插件treeTable(转)
由于工作需要,要直观的看到某个业务是由那些子业务引起的异常,所以我需要用树表的方式来展现各个层次的数据. 需求: 1.数据层次分明: 2.数据读取慢.需要动态加载孩子节点: 3.支持默认展开多少层. ...
- ASP.NET中使用jQuery插件实现图片幻灯效果
参照网上的资料及提供的jQuery插件实现图片幻灯效果. 1.页面前台代码: //头部引用 <head runat="server"><title>< ...
- JQuery插件让图片旋转任意角度且代码极其简单 - 摘自网友
JQuery插件让图片旋转任意角度且代码极其简单 2012-04-01 09:57:03 我来说两句 收藏 我要投稿 引入下方的jquery.rotate.js文件,然后通过$ ...
- jQuery插件实现图片展开效果,jquery.gallery。仿腾讯QQ空间说说图片展示效果。
公司的项目http://www.umfun.com/,有个说说的页面(和腾讯QQ空间说说一样),里面有个发表图片功能,上传完图片,需要点击展开的效果. 当时手里面事情比较多(公司就我一个前端),忙不过 ...
- 使用jquery插件实现图片延迟加载技术(懒加载)
有时我们看到一些大型网站,页面如果有很多图片的时候,当你滚动到相应的行时,当前行的图片才即时加载的,这样子的话页面在打开只加可视区域的图片,而其它隐藏的图片则不加载,一定程序上加快了页面加载的速度,对 ...
- JQuery插件之图片轮播插件–slideBox
来源:http://www.ido321.com/852.html 今天偶然发现了一个比较好用的图片轮播插件—slideBox 先看看效果:http://slidebox.sinaapp.com/ 代 ...
- JQuery插件:图片上传本地预览插件,改进案例一则。
/* *名称:图片上传本地预览插件 v1.1 *作者:周祥 *时间:2013年11月26日 *介绍:基于JQUERY扩展,图片上传预览插件 目前兼容浏览器(IE 谷歌 火狐) 不支持safari *插 ...
- 使用jquery插件实现图片延迟加载--懒加载技术
原文链接:http://www.cnblogs.com/lei2007/archive/2013/05/31/3110725.html 感谢作者.以下为原文,备忘仅供自己学习. 第一:lazyLoad ...
- JQuery插件让图片旋转任意角度且代码极其简单
引入下方的jquery.rotate.js文件,然后通过$("选择器").rotate(角度);可以旋转任意角度, 例如$("#rotate-image").r ...
随机推荐
- MIME简介
MIME(Multipurpose Internet Mail Extensions)多用途互联网邮件扩展类型.是设定某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访问的时候,浏览器 ...
- Win下执行Swing程序的BAT文件 和 Linux下执行Swing程序的SH文件
BAT文件: @echo off set CLASSPATH_BAK=%CLASSPATH% set classpath=%CLASSPATH%;.\lib\commons-codec-1.3.jar ...
- CSS兼容性解决方法!important的IE7,Firefox问题
转自:http://www.codesky.net/article/201008/139903.html 1. 首先谈谈!important问题的引起(盒模型问题): 在CSS标准中,一个盒模型包括4 ...
- Java计算机硬盘大小转换(B,KB,MB,GB,TB,PB之间的大小转换)
程序员都很懒,你懂的! java程序员在实际的开发中会遇到很多的单位换算问题.今天我给大家带来的是关于计算机硬盘大小的换算.多数情况下,一般要求 b,kb,mb,gb,tb,pb之间的大小转换,我们都 ...
- react-router-dom Link search 传参
<Link> 和之前版本没太大区别,重点看下组件属性: to(string/object):要跳转的路径或地址: replace(bool):为 true 时,点击链接后将使用新地址替换掉 ...
- windows配置meld
meld 官网:http://meldmerge.org/ git配置: git bash: git config --global merge.tool meld ...
- vsCode 添加浏览器调试和js调试的方法总结
vsCode 添加浏览器调试和js调试的方法 1.直接按F5可以调试的方法或者点击运行按钮(可以直接运行html文件或者js文件) 在launch.json文件中的配置如下: { " ...
- state Threads 开源库介绍
译文在后面. State Threads for Internet Applications Introduction State Threads is an application library ...
- 关于flex,好像有12个属性非常重要
关于Flex,有12个属性非常重要 这几天在学习Flex布局,发现Flex真的好厉害! Flex是Flexible Box的缩写,意为"弹性布局",用来为盒模型提供最大的灵活性. ...
- Windows Mobile自动更新
private static string m_CurrentPath; //取得作业平台 private static string Platform { get { return Environm ...