javascript的未知尺寸图片保持比例水平垂直居中函数
JavaScript的图片在容器内水平垂直居中的函数,利用图片加载获取图片大小,使之在父节点内水平垂直居中
展示方式有两种:
1、当参数keepImageFull为true:保持图片比例,使图片可完整的水平居中显示在父节点内,未能填充的部分留白
2、当参数keepImageFull为false: 保持图片比例,完全覆盖父节点,超出部分溢出
container_width和container_height函数不是必须的,仅当无法自动获取到图片父节点的尺寸时传入
容器内不想用此方法处理的函数的图片,可以在图片节点上加上data-skip-loader属性
使用实例:
<div id="container">
<ul>
<li><img src="1.png" alt=""></li>
<li><img src="1.png" alt=""></li>
<li><img src="1.png" alt=""></li>
<li><img src="1.png" alt=""></li>
</ul>
<img src="" alt="" class="ignore-element" data-skip-loader="1">
</div>
<script>
imageCenter(document.getElementById('container'), false, 300, 200);
</script>
具体看参数说明:
/**
* 设置元素样式
* @param {Array|Element} element 要设置样式的dom元素
* @param {Object} styles 样式属性集合
*/
function setStyle(element, styles) {
var cssText = [];
if (styles) {
for (var prop in styles) {
if (styles.hasOwnProperty(prop)) {
cssText.push([prop, styles[prop]].join(':'));
}
}
} if (cssText.length) {
element.style.cssText += ';' + cssText.join(';');
} return element;
} /**
* 图片预加载
* @param {Element} node 要加载的容器节点
* @param {Function} after_single 单词加载成功执行函数
* @param {Function} after_all <可选> 全部成功执行函数
* @param {Object} context <可选> 函数运行域
*/
function loadImage(node, after_single, after_all, context) {
function load_check(loading) {
if (loading["complete"]) {
complete++;
setStyle(loading.ele, {
'width': 'auto',
'max-width': 'none',
'max-height': 'none'
});
if (typeof after_single === 'function') {
after_single.call(context, loading.ele, loading.width || 1, loading.height || 1, loading.i);
}
if (complete == filter_images.length && typeof after_all === 'function') {
after_all.call(context, complete);
}
loading.ele = null;
loading = null;
} else {
setTimeout(function(){
load_check(loading);
}, 100);
}
}
node = node || document; var images = node.getElementsByTagName("img");
var complete = 0;
// 过滤
var filter_images = [];
for (var j = 0; j < images.length; j++) {
if (!images[j].hasAttribute('data-skip-loader')) {
filter_images.push(images[j]);
}
} var i = 0;
while(i < filter_images.length) {
var image = filter_images[i];
var loading = new Image();
setStyle(image, {
'width': '100%',
'height': 'auto',
'max-width': '100%',
'max-height': '100%'
});
loading.src = image.src;
loading.ele = image;
loading.i = i;
load_check(loading);
loading = null;
i++;
}
} /**
* 图片水平垂直居中于容器中
* @param {Element} node 容器节点
* @param {Boolean} keepImageFull 保持图片全图可见,未能填充的部分留白 || 以长宽的最小值填满,未能显示的部分溢出影藏
* @param {Number} container_width <可选> 容器宽度
* @param {Number} container_height <可选> 容器高度
*/
function imageCenter(node, keepImageFull, container_width, container_height) {
loadImage(node, function(img, width, height, index) {
var parent = img.parentNode;
if (!container_width) {
container_width = Math.max(1, parent.offsetWidth);
}
if (!container_height) {
container_height = Math.max(1, parent.offsetHeight);
}
setStyle(parent, {
'overflow': 'hidden'
});
var css = {
'display': 'block',
'border-width': 0
};
css['width'] = Math.max(1, Math.min(width, container_width));
css['height'] = css["width"] * height / width; keepImageFull = keepImageFull ? 1 : -1; // 宽度修正
if ((css['width'] - Math.min(width, container_width)) * keepImageFull > 0) {
css['width'] = container_width;
css['height'] = height * container_width / width;
} // 高度修正
if ((css['height'] - Math.min(height, container_height)) * keepImageFull > 0) {
css['width'] = width * container_height / height;
css['height'] = container_height;
} css['margin-left'] = (container_width - css['width']) / 2 + 'px';
css['margin-top'] = (container_height - css['height']) / 2 + 'px';
css['width'] += 'px';
css['height'] += 'px'; setStyle(img, css);
});
}
javascript的未知尺寸图片保持比例水平垂直居中函数的更多相关文章
- 单个未知大小图片在div里面垂直居中的方法。。。添加辅助元素挤一下位置达到居中
单个未知大小图片在div里面垂直居中的方法...添加辅助元素挤一下位置达到居中 <div class="ServicesLiTopPic"> <i>&l ...
- CSS中如何实现未知尺寸图片垂直居中
在曾经的 淘宝UED 招聘 中有这样一道题目: “使用纯CSS实现未知尺寸的图片(但高宽都小于200px)在200px的正方形容器中水平和垂直居中.” 当然出题并不是随意,而是有其现实的原因,垂直居中 ...
- 纯css实现div中未知尺寸图片的垂直居中
1.淘宝的方法 在曾经的"淘宝UED招聘"中有这样一道题目: “使用纯CSS实现未知尺寸的图片(但高宽都小于200px)在200px的正方形容器中水平和垂直居中.” 当然出题并不是 ...
- table-cell实现未知宽高图片,文本水平垂直居中在div
<BODY> <h1>未知宽高的图片水平垂直居中在div</h1> <!--box-outer--> <div class="box-o ...
- 未知高度-纯css实现水平垂直居中
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 图片和span水平垂直居中
<style type="text/css"> .content{ width:20%; height:60px; border:1px solid red; text ...
- css中:如何让一个图片(不知道宽高,宽高可能比父元素div大),在父元素div内部水平垂直居中,并且不溢出父元素div,且图片不拉伸变形(可等比例缩小)?
欢迎进入:http://www.jscwwd.com/article/list/%E5%85%A8%E9%83%A8 效果图: 不管父元素的宽高怎么变化,图片都是水平垂直居中的,并且不溢出父元素. 注 ...
- 前端面试题:CSS实现水平垂直居中
这是一个挺常见的前端面试题,但是没有做过总结.有的时候可能会使用完了,很长一段时间不去使用,会慢慢忘记.所以,温故而知新,还是很有必要的. 一.绝对定位元素的居中实现 这一种工作中用的应该是最多的,兼 ...
- CSS水平垂直居中总结
行内元素水平居中 把行内元素包裹在块级父元素中,且父元素中的css设置text-align:center; <!DOCTYPE html> <html> <head> ...
随机推荐
- ASP.NET MVC 1.0 参考源码索引
http://www.projky.com/asp.netmvc/1.0/System/Web/Mvc/AcceptVerbsAttribute.cs.htmlhttp://www.projky.co ...
- JPEG标准推荐的亮度、色度DC、AC Huffman编码表
JPEG 标准推荐的亮度.色度DC.AC Huffman 编码表 博主在完成数字图像处理大作业时利用搜索引擎查找了很久完整的四张Huffman 编码表(亮度AC Huffman编码表.亮度DC Huf ...
- 开放地址法散列表ADT
数据结构定义如下: typedef unsigned int Index; typedef Index Position; struct HashTbl; typedef struct HashTbl ...
- 获取ios设备的udid
今天get的第二个技能~~~ UDID指的是设备的唯一设备识别符,ipa包未上架之前如果不添加udid是无法安装成功的.那么如何快速获取ios设备的udid呢? 今天get的方法是用蒲公英,网址:ht ...
- 9款最佳的Linux文件比较工具
程序员和撰稿人在编写程序文件或平常的文本文件时,有时想知道两个文件或同一文件的两个版本之间的差异.你在Linux上比较两个计算机文件时,文件内容之间的差异就叫diff.这一描述来源于提到diff的输出 ...
- MachineLearning Exercise 4 :Neural Networks Learning
nnCostFunction 消耗公式: a1 = [ones(m,) X]; z2 = a1*Theta1'; pre = sigmoid(a1*Theta1'); a2 = [ones(m,) p ...
- Python 变量(下)
列表 列表是可修改的序列类型.所以列表不可以作为字典的键. >>> a = [1] >>> hash(a) Traceback (most recent call ...
- C++模式学习------代理模式
Proxy代理模式 : 为其他对象提供一种代理以控制对这个对象的访问.代理类作为桥梁是请求方和执行方的中间者,将请求方和真正的执行方分割开来,也是两者之间调用的协调者.例如执行类也就是被代理类,可以在 ...
- CodeForces - 988D(思维STL)
原文地址:https://blog.csdn.net/weixin_39453270/article/details/80548442 博主已经讲的很好了 题意: 从一个序列中,选出一个集合,使得集合 ...
- Little Elephant and Array CodeForces - 220B(莫队)
给一段长为n的序列和m个关于区间的询问,求出每个询问的区间中有多少种数字是 该种数字出现的次数等于该数字 的. #include <iostream> #include <cstdi ...