js控制html页面显示图片方式,只需要引入“jquery-1.11.2.min.js”

js:

/*
引用
<script src="jquery-1.11.2.min.js" type="text/javascript"></script>
<script src="showImage.js" type="text/javascript"></script>
<script>
$(function () {
showImage.init({Div:"div.showImage"});
}); </script>
外层div建议设置宽高,div默认宽度为父级宽度,但高度未知
<div class="showImage" style="width:350px;height:300px;background-color:Black;">
<img style="" src="http://photocdn.sohu.com/20170328/Img485277054.jpg"></img>
</div>
*/
var showImage = {
item: {
Div: "div.showImage", //获取外层div的选择器
Img: "img:eq(0)", //从div子元素 获取图片的选择器
isCenter: true, //是否居中
showBeyond: false, //是否显示超出部分
fixedWH: "delfault", /*固定宽或高:
"delfault"大图图片小边,小图固定图片大边,宽图高图不变大小
"abs"取宽高差值最小固定。
"width"固定宽。
"height" 固定高。*/
full: false//小图放大充满 外层div },
init: function (item) {
showImage.item = $.extend({}, showImage.item, item); var d = $(showImage.item.Div);
if (d.length > 0) { for (var i = 0; i < d.length; i++) {
showImage.showImage(d[i]);
}
} else {
showImage.showImage(d[0]);
}
},
showImage: function (obj, width, height) {
if (obj == undefined) {
return;
}
//obj:外层div对象
obj = $(obj);
//超出外层div部分不显示
if (!showImage.item.showBeyond) {
obj.css("overflow", "hidden");
}
//清除外层div样式,对图片定位样式
obj.css("padding", "0px 0px 0px 0px"); //获取div大小
if (!width) {
width = obj.get(0).offsetWidth;
}
if (!height) {
height = obj.get(0).offsetHeight;
}
if (height <= 0 && width <= 0) {
return;
}
//获取图片
var img = obj.children(showImage.item.Img).get(0); //设置图片大小,位置 //图片加载完成
if (img.complete) {
showImage.LocationImg(img, width, height);
} else {
//图片未加载
img.onload = function () { showImage.LocationImg(img, width, height); };
} },
LocationImg: function (img, width, height) {
img = $(img);
var img2 = new Image();
img2.src = img.get(0).src; //设置图片大小
//获取图片宽高
var imgheight = img2.height;
var imgwidth = img2.width; // var imgheight = img.get(0).offsetHeight;
// var imgwidth = img.get(0).offsetWidth;
// var imgwidth = img.get(0).naturalWidth;
// var imgheight = img.get(0).naturalHeight;
if (!img.complete) {
// 图片标签尚未加载
setTimeout(function () {
//设置图片显示
showImage.LocationImg(img, width, height);
}, 1000);
return;
}
//height 外层div高, width 外层div宽
width = parseFloat(width);
height = parseFloat(height);
imgwidth = parseFloat(imgwidth);
imgheight = parseFloat(imgheight); if (width == 0) {
width = imgwidth;
}
if (height == 0) {
height = imgheight;
}
//固定宽或高,另一边等比缩放
if (showImage.item.fixedWH == "width") {
//等比缩放高
imgheight = imgheight * (width / imgwidth);
//固定宽
imgwidth = width;
} else if (showImage.item.fixedWH == "height") {
//等比缩放宽
imgwidth = imgwidth * (height / imgheight);
//固定高
imgheight = height;
} else if (showImage.item.fixedWH == "delfault") {
//大图图片小边,小图固定图片大边,宽图高图不变大小
if (imgheight >= height && imgwidth >= width) {
//大图充满
if (imgheight * (width / imgwidth) >= height) {
//等比缩放高
imgheight = imgheight * (width / imgwidth);
//固定宽
imgwidth = width;
} else {
//等比缩放宽
imgwidth = imgwidth * (height / imgheight);
//固定高
imgheight = height;
}
} else if (imgheight < height && imgwidth < width) {
//小图
if (imgwidth < imgheight) {
//等比缩放宽
imgwidth = imgwidth * (height / imgheight);
//固定高
imgheight = height;
} else if (imgwidth == imgheight) {
if (height > width) {
//等比缩放高
imgheight = imgheight * (width / imgwidth);
//固定宽
imgwidth = width;
} else {
//等比缩放宽
imgwidth = imgwidth * (height / imgheight);
//固定高
imgheight = height;
}
} else {
//等比缩放高
imgheight = imgheight * (width / imgwidth);
//固定宽
imgwidth = width;
}
} else {
//高图或宽图
//不变大小,留白
}
} else if (showImage.item.fixedWH == "abs") {
//宽差值,高差值比较
if (Math.abs(height - imgheight) > Math.abs(width - imgwidth)) {
//等比缩放高
imgheight = imgheight * (width / imgwidth);
//固定宽
imgwidth = width;
} else {
//等比缩放宽
imgwidth = imgwidth * (height / imgheight);
//固定高
imgheight = height;
}
}
//充满
if (showImage.item.full) {
if (imgheight == height && imgwidth < width) {
//使宽充满
imgwidth = width * (width / imgwidth);
imgheight = width; } else if (imgheight < height && imgwidth == width) {
//使高充满
imgwidth = imgwidth * (height / imgheight);
imgheight = height; } }
//设置图片宽高
img.css("height", (100 * imgheight / height) + "%");
img.css("width", (100 * imgwidth / width) + "%"); //图片定位样式
img.css("float", "left");
img.css("position", "relative");
img.css("bottom", "0px");
img.css("right", "0px");
img.css("margin", "0px 0px 0px 0px");
img.css("padding", "0px 0px 0px 0px"); if (showImage.item.isCenter) {
//定位居中
var toppx = (((100 * (Math.abs(height - imgheight)) / height)) / 2).toFixed(2) + "%";
var leftpx = (((100 * (Math.abs(imgwidth - width)) / width)) / 2).toFixed(2) + "%"; if (imgwidth > width) {
leftpx = "-" + leftpx;
}
if (imgheight > height) {
toppx = "-" + toppx;
}
img.css("left", leftpx);
img.css("top", toppx);
} } //设置图片大小, 位置
};

html:

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head >
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="jquery-1.11.2.min.js" type="text/javascript"></script>
<script src="showImage.js" type="text/javascript"></script>
<script>
$(function () {
showImage.init({ Div: "div.showImage" });
});
</script> </head>
<body>
<form id="form1" runat="server" style="padding-left:100px;">
长图
<br />
<div class="showImage" style="width:100px;height:100px;background-color:Black;">
<img style="" src="https://test2015data.oss-cn-hangzhou.aliyuncs.com/image-hroot/year20173/image-201730/news/file_170328203053104559.png"></img>
</div>
<br />
<br />
<br />
<br />
<br />
<br />
长图原图
<br />
<img style="" src="https://test2015data.oss-cn-hangzhou.aliyuncs.com/image-hroot/year20173/image-201730/news/file_170328203053104559.png"></img>
<br />
<br />
高图
<br />
<div class="showImage" style="width:350px;height:300px; background-color:Black;">
<img style="position:inherit;" src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1490965812249&di=7ec87be2d7b63733a7bba492e952202e&imgtype=0&src=http%3A%2F%2Fwww.shuhua365.com%2Fhualang%2Fuploadfile%2F2010312112959216.jpg"></img> </div>
<br />
高图原图
<br />
<img style="" src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1490965812249&di=7ec87be2d7b63733a7bba492e952202e&imgtype=0&src=http%3A%2F%2Fwww.shuhua365.com%2Fhualang%2Fuploadfile%2F2010312112959216.jpg"></img> </form>
</body>
<form id="form1" style="padding-left:100px;">
大图
<br />
<div class="showImage" style="width:350px;height:300px;background-color:Black;">
<img style="width:200px;height:200px;" src="http://photocdn.sohu.com/20170328/Img485277054.jpg"></img>
</div>
<br />
<br />
<br />
<br />
<br />
<br />
大图原图
<br />
<img style="" src="http://photocdn.sohu.com/20170328/Img485277054.jpg"></img>
<br />
<br />
小图
<br />
<div class="showImage" style="width:350px;height:300px; background-color:Black;">
<img style="position:inherit;" src="https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=512897042,748871735&fm=80&w=179&h=119&img.JPEG"></img> </div>
<br />
小图原图
<br />
<img style="" src="https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=512897042,748871735&fm=80&w=179&h=119&img.JPEG"></img> </form>
</body>
</html>

【js】【图片显示】js控制html页面显示图片方式的更多相关文章

  1. 原生JS的移入溢出控制div的显示与隐藏

    原生JS的移入溢出控制div的显示与隐藏的写法 上面的写法火狐存在兼容性

  2. uploadify实现七牛云存储 显示上传进度+页面显示

    准备: uploadify下载地址: http://www.uploadify.com/download/ 七牛 php-sdk开发指南: http://developer.qiniu.com/doc ...

  3. 无阻塞加载js,防止因js加载不了影响页面显示

    浏览器加载静态资源和js的方式都是线性加载,所以一般情况可以将js放到</body>前,防止UI线程的阻塞. 而某些时候我们既希望js在整个网页的头部就加载,又担心js阻塞导致网站加载缓慢 ...

  4. JS实现让滚轮控制网页头部显示与隐藏

    在很多网站中都有鼠标网上滚动头部就会滑出,继续往下滚动就会隐藏,下面看看实现方法 scroll(); function scroll(){// 入口方法 这个方法是获取事件的兼容,获取delta -- ...

  5. JS判断IE版本并在页面显示内容

    <script type="text/javascript"> var isIE = function (ver) { var b = document.createE ...

  6. Django实现图片上传并前端页面显示

    Django实现图片上传和图片显示 开始之前我们先确认环境中已经安装了Pillow,如果没有安装,可以通过pip install Pillow来安装,这个是python的图像处理库 数据库设置 我们创 ...

  7. 图片转成base64位 页面中图片展示

    <img src="data:image/gif;base64,/9j/4QAYRXhpZgAASUkqAAgAAAAAAAAAAAAAAP/sABFEdWNreQABAAQAAABG ...

  8. jquery控制左右箭头滚动图片列表

    jquery控制左右箭头滚动图片列表的实例. 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&q ...

  9. js控制页面显示和表单提交

    早期的web页面在显示方面一般在后台进行控制,虽然对后台开发来讲是比较容易做到的,但是涉及到一个问题,那就是数据库压力. 因为要控制显示,所以会比较频繁的从数据库中来回调用. 现在的js功能越来越强, ...

随机推荐

  1. Python练习:含参数的脚本示例

    首先准备一个example.csv文件,如下: 编写脚本test.py ,实现传入参数,读取example.csv文件,并将其保存为另一个文件, #  含参数的脚本,读取一个文件,并另保存一个文件im ...

  2. Grid move

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  3. C# 弹出确定、取消窗口

    if (MessageBox.Show("确定要退出吗?", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Qu ...

  4. Machine Learning 第一二周

    # ML week 1 2 一.关于machine learning的名词 学习 从无数数据提供的E:experience中找到一个函数使得得到T:task后能够得到P:prediction 监督学习 ...

  5. C博客作业06--结构体&文件

    1.本章学习总结 1.1思维导图 1.2本章学习体会 学习了结构和文件,又是懵懵的课了,我的天啊.结构还好,题目集一出就做了,不是很难,感觉掌握的还可以,不过这只是感觉而已,等到真正来写大作业的时候又 ...

  6. js判断终端以及APP应用判断

    **第一种:通过判断浏览器的userAgent,用正则来判断是否是ios和Android客户端.代码如下:** <script type="text/javascript"& ...

  7. 最长(大)回文串的查找(字符串中找出最长的回文串)PHP实现

    首先还是先解释一下什么是回文串:就是从左到右或者从右到左读,都是同样的字符串.比如:上海自来水来自海上,bob等等. 那么什么又是找出最长回文串呢? 例如:字符串abcdefedcfggggggfc, ...

  8. hiberate 映射关系 详解

    在我们平时所学的关系型数据库中,我们会大量处理表与表之间的关系,如果表比较多的话处理起来就比较繁琐了,但是hibernate给我们提供了很大的便利,这些便利让我们处理起来方便.我们所讲的源码地址:ht ...

  9. freopen()函数在ACM中的使用

    #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif https://blog.csdn.net/c ...

  10. Pandas数据处理+Matplotlib绘图案例

    利用pandas对数据进行预处理然后再使用matplotlib对处理后的数据进行数据可视化是数据分析中常用的方法. 第一组例子(星巴克咖啡店) 假如我们现在有这样一组数据:星巴克在全球的咖啡店信息,如 ...