本篇教程介绍了jQuery教程 css+jQuery实现图片局部放大预览,希望阅读本篇文章以后大家有所收获,帮助大家对jQuery的理解更加深入。

<

今天有时间开始动手,使用css+jqueryshi实现了图片局部放大的组件,首先看看效果图:

界面设计思路如下:

1.两个div,左边放图片的缩略图   2.在左边缩略图鼠标移动的时候,区域(效果图中的网格)   3.右边放大图div,背景为缩略图的大图,在鼠标移入缩略图中,通过获取鼠标的坐标,将右侧背景图片移动到跟鼠标坐标对应的位置   4.设计时尽量,获取原大图的尺寸,和缩略图视窗计算比例,更大精度的做到左侧缩略图上表示的区域,和右侧放大部分匹配

本示例大部分编码在javascript脚本,以下列出各部分的源码:

css:

.all-region {

width: 100%;

height: auto;

}

.all-region .image-wrap {

width: 1300px;

margin: 0px auto;

height: 300px;

line-height: 300px;

overflow: hidden;

vertical-align: center;

background: #FBFBFB;

border-left: 1px solid #ebebeb;

position: relative;

}

.image-wrap .little-img img {

width: 600px;

height: 300px;

}

.image-wrap .large-img {

width: 600px;

height: 300px;

background-image: url("./images/show-window/timg.jpg");

border: 1px solid transparent;

background-size: inherit;

background-repeat: no-repeat;

background-position-x: 0px;

background-position-y: 0px;

position: relative;

top: -301px;

left: 605px;

float: left;

z-index: 10;

opacity: 0;

display: none;

transition: opacity 2s linear;

overflow: hidden;

}

.relative-region {

background: linear-gradient(45deg, rgba(46, 46, 46, 0.5) 50%, transparent 0),

linear-gradient(135deg, rgba(200, 200, 200, .5) 70%, transparent 0);

display: none;

position: relative;

z-index: 99;

background-size: 5px 5px;

}

Javascript:

class Elements {

constructor() {

//缩略图区域

this.sourceImage = $(".little-img");

//放大区域

this.aimImage = $(".large-img");

//视图图片,与图片实际尺寸比例

this.sourceToAimRate = 0.01;

//原图高度

this.sourceHeight = 0;

//原图宽度

this.sourceWidth = 0;

//视图高度,div的高度,本例是300px

this.containerHeight = this.sourceImage.children().height();

this.containerWidth = this.sourceImage.children().width();

//鼠标在缩略图上的坐标 offsetX

this.cursor_x = 0;

this.cursor_y = 0;

//标志被放大的区域

this.region = $(".relative-region");

this.mouseMove = this.mouseMove.bind(this);

this.regionPosition = this.regionPosition.bind(this);

this.regionMove = this.regionMove.bind(this);

this.caculatorRate = this.caculatorRate.bind(this);

}

//计算原图尺寸,思路是内存加载原图,获得尺寸,并计算容器视图和原图的比例

caculatorRate() {

console.log(this.sourceImage.children().attr("src"));

$("").attr("src", this.sourceImage.children().attr("src")).load((e) => {

//let sourceImageWidth=e.target.width;

this.sourceWidth = e.target.width;

this.sourceHeight = e.target.height;

//计算图片和容器的像素比例

this.sourceToAimRate = this.sourceWidth / this.containerWidth;

});

}

//鼠标在缩略图上移动时计算,放大图的背景位置,并且定位标识被放大的区域

mouseMove(e) {

//console.log(`x:${e.offsetX},y:${e.offsetY}`);

//偏离region的位置

//由于鼠标实际上是在标识被放大区域(网格区域)的div里面,所以通过e获取的实际上是缩略图内,网格标识的offsetX 要用网格区域的offsetX+offsetLeft-缩略图的offsetleft才是鼠标对应到缩略图上的位置

let r_x = e.offsetX;

let r_y = e.offsetY;

let s_t = this.sourceImage.offset().top;

let s_l = this.sourceImage.offset().left;

let r_t = this.region.offset().top;

let r_l = this.region.offset().left;

let x = r_l - s_l + r_x;

let y = r_t - s_t + r_y;

//在原图上显示,被放大的区域

let w = this.region.width();

let h = this.region.height();

//由于鼠标在网格区域的中心,所以在计算放大图的top和left的时候,实际是从网格的左上角位置

this.cursor_x = (x - w / 2) * this.sourceToAimRate;

this.cursor_y = (y - h / 2) * this.sourceToAimRate;

if (this.cursor_x + this.containerWidth > this.sourceWidth) {

this.cursor_x = this.sourceWidth - this.containerWidth;

}

if (this.cursor_y + this.containerHeight > this.sourceHeight) {

this.cursor_y = this.sourceHeight - this.containerHeight;

}

if (this.cursor_y

this.cursor_y = 0;

}

if (this.cursor_x

this.cursor_x = 0;

}

this.aimImage.css({

"background-position-x": -this.cursor_x + "px",

"background-position-y": -this.cursor_y + "px"

});

this.regionMove(w, h, x, y);

}

regionPosition(r_w, r_h, e) {

let left = e.offsetX - r_w / 2;

let top = e.offsetY - r_h / 2;

if (left

left = 0;

}

if (left + r_w > this.containerWidth) {

left = this.containerWidth - r_w;

}

if (top

top = 0;

}

if (top + r_h > this.containerHeight) {

top = this.containerHeight - r_h;

}

this.region.css({

"top": (top - this.containerHeight) + "px",

"left": left+ "px",

"cursor": "crosshair"

});

}

regionMove(r_w, r_h, x, y) {

let left = x - r_w / 2;

let top = y - r_h / 2;

if (left

left = 0;

}

if (left + r_w > this.containerWidth) {

left = this.containerWidth - r_w;

}

if (top

top = 0;

}

if (top + r_h > this.containerHeight) {

top = this.containerHeight - r_h;

}

this.region.css({"top": (top - this.containerHeight) + "px", "left": left + "px"});

}

init() {

this.caculatorRate();

//鼠标移入缩略图区域,由缩略图区域的hover事件初始化,将鼠标放入网格区域的中心

this.sourceImage.children().mouseover((e) => {

this.aimImage.css({"display": "block", "opacity": "1"});

let r_w = this.containerWidth / this.sourceToAimRate;

let r_h = this.containerHeight / this.sourceToAimRate;

let x = e.offsetX;

let y = e.offsetY;

this.regionPosition(r_w, r_h, e);

this.region.css({"display": "block", "height": r_h + "px", "width": r_w + "px"});

});

//修复鼠标在region上,右侧放大区域闪动

this.region.mousemove(this.mouseMove);

this.region.mouseleave(() => {

this.aimImage.css({"display": "none", "opacity": "0"});

this.region.css({"display": "none"});

});

}

}

$(function () {

var e = new Elements();

e.init();

})

由于原图是1920*1080不是缩略视窗严格的2:1,计算中有小数等原因,使网格标识的区域,和放大区域展示的完全匹配

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标WEB前端jQuery频道!

html图片点击局部放大,jQuery教程 css+jQuery实现图片局部放大预览相关推荐

  1. php背景图片平铺拉伸,利用jQuery和CSS将背景图片拉伸

    现在WEB页面设计比较流行使用大背景图,那么您知道如何使用一张大背景图进行拉伸效果呢?也就是说使用一张固定尺寸的背景图片,让它在页面中随着浏览器尺寸进行拉伸,就像我们的电脑桌面壁纸效果.本文将带您一起 ...

  2. pdfh5.js 基于pdf.js和jQuery,web/h5/移动端PDF预览手势缩放插件。

    pdfh5.js 基于pdf.js和jQuery,web/h5/移动端PDF预览手势缩放插件. 注意:本地绝对路径地址不能加载,跨域问题用代理或者服务端解决. svg模式渲染存在缺陷,只能渲染普通pd ...

  3. imgareaselect + php 裁剪和上传,jQuery插件ImgAreaSelect实现头像上传预览和裁剪功能

    本文主要介绍了jQuery插件ImgAreaSelect实现头像上传预览和裁剪功能实例讲解一,需要的朋友可以参考下,希望能帮助到大家. 上一节随笔中,我们已经知道了关于jQuery插件ImgAreaS ...

  4. js和css实现手机横竖屏预览思路整理

    实现效果,如上图. 首先,实现手机页面在PC端预览, 则先在网上找到一个手机的背景图片,算好大概内间距,用来放预览的页面,我这里是给手机预览页面的尺寸按iphone5的尺寸来的: 一个手机页面在这里预 ...

  5. Xamarin XAML语言教程Visual Studio中实现XAML预览

    2019独角兽企业重金招聘Python工程师标准>>> Xamarin XAML语言教程Visual Studio中实现XAML预览 每次通过编译运行的方式查看XAML文件效果,需要 ...

  6. css样式教程---css控制背景图片-背景相关的css

    最近做网站的时候,感觉css使用并不是那么轻松,于是从网上找了点教程看看,感觉还是挺重要的,一个好的CSS代码,只要你熟悉了,以后拿过来之间套上就行了, 真是方面,但是看的那CSS代码总叫人感觉头大, ...

  7. php jquery教程下载,jquery怎么下载和引用

    下载和引用jquery的方法:首先进入jQuery的官网:然后找到jQuery官网首页右侧的下载按钮:接着点击进入jQuery的下载页面并进行下载:最后用script标签引入jquery即可. 本教程 ...

  8. html图片的宽度和高度设置,CSS设置img图片的宽度和高度

    如何使用CSS控制HTML中图片高度宽度,统一对象内图片高度宽度等样式属性? 我们在布局图片列表时,通常我们要控制img图片的高度和宽度这样来达到图片统一. 如以下这个的图片列表布局示图 图片img高 ...

  9. jquery关于css,jQuery css操作

    jQuery操作css的元素样式 1.访问匹配元素的样式属性 来个小案例: //宽高都是200px,背景颜色红色; 怎么获取div的属性值呢: $("#div").css(&quo ...

最新文章

  1. 从.NET1.1升级到.NET2.0时出现的PInvokeStackImbalance错误
  2. 2021年, 别再只沉迷于GANs 和 Transformer,GNN爆发已经从CV蔓延到物理化学
  3. 揭秘物联网之城无锡鸿山的科技密码
  4. Cisco认证(CCNA,CCNP,CCIE)体系中文书籍全集书籍
  5. A Java Runtime Environment (JRE) or Java Development Kit (JDK)
  6. 使用studio2005写单元测试
  7. android毛玻璃遮罩效果_css3毛玻璃效果[模糊图片]
  8. LeetCode动态规划 环形子数组的最大和
  9. 计算机数值计算的相关文章,数值计算论文.doc
  10. struts 的radio标记的增强
  11. onnx-tensorrt:builtin_op_importers.cpp:628:5: error: ‘IIdentityLayer’ is not a member of ‘nvinfer1’
  12. 楼板计算塑形弹性_阶梯教室板模板支架工程方案计算书(仅供参考)
  13. Adobe Acrobat Pro制作pdf模板
  14. 商标是否占用查询方法、阿里云商标注册方法
  15. 通过Jsoup 和 htmlunit 爬取全国行政区划信息查询平台的省市区区划数据
  16. QQ特殊字符制作方式
  17. php启动后no input file specified.,php网站出现no input file specified 三种解决方法
  18. OpenHarmony更新编译问题及解决办法
  19. 如何看hbo_如何取消您的HBO Now帐户
  20. Hive hql 经典5道面试题

热门文章

  1. Blender基础技巧小结
  2. SQL小白超级推荐自学入门书籍
  3. OpenAI ChatGPT API + FaskAPI SSE Stream 流式周转技术 以及前端Fetch 流式请求获取案例
  4. 基于springboot的微信公众号JSAPI支付
  5. 生成式AI发现潜在抗癌药物;王慧文与“一流科技”达成并购意向;阿里巴巴公布六大业务集团CEO丨每日大事件...
  6. python作业习题
  7. 老兵不死(麦克阿瑟的告别演讲)
  8. 【计算机网络】TCP协议经典十五连问(半连接、重传、滑动窗口、流量窗口、拥塞控制、SYN Flood攻击、粘包拆包)
  9. 电气工程及其自动化就业方向及前景
  10. 搭建Keil编程环境,ARM汇编语言编程入门实践