javascript仿新浪微博图片放大缩小及旋转效果

经常看到新浪微博里有图片放大缩小旋转效果,感觉效果还不错,所以就想试着做一个类似的demo出来,至于旋转对于IE可以用滤镜来解决,标准的浏览器可以用html5中的canvas画布来解决。

思路:1.点击小图后,小图隐藏掉,在小图父级元素后增加一张大图且显示出来。

2.点击往左转,往右转触发旋转方法。

3. 点击收起按钮,把1的步骤反过来 隐藏大图 显示小图。

4. 点击查看原图功能 目前没有做成js灯箱效果,直接打开一个新连接。但是如果想做成灯箱效果的话,可以看我这篇博客,灯箱效果演示

我们可以先来看看JSFiddler效果吧!

嘿嘿,看效果请点击我!

实现思路:

1. 对于第一点 小图隐藏 大图显示这个可以不用说的。

2. 对于旋转:IE用滤镜解决,如:img.style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(rotation=' + s + ')'; 标准浏览器可以用canvas+旋转

代码中用了一个图片预加载,想要了解图片预加载的话 可以看这篇文章  "图片预加载"  还用了一个图片等比例缩放 想了解等比例缩放的话 可以查看 "等比例缩放图片"

下面是所有的JS源码,大家有空可以看看,如果有更好的思路的话,可以提意见,一起交流下。

/**
* 缩略图
*/
function ArtZoom(options,callback){
var self = this;
self.options = $.extend({},defaults,options || {});
self._init();
self.callback = callback;
this.cache = {
minStep : 0,
maxStep : 3
};
};
$.extend(ArtZoom.prototype,{ // 初始化
_init: function(){
var self = this,
cfg = self.options; if($(cfg.targetCls).length <= 0) {
return;
}
$(cfg.targetCls).each(function(){
$(this).unbind('click').bind('click',function(e){
e.preventDefault();
var maxImage = $(this).attr('href');
self._imgTool($(this),maxImage);
});
});
},
/*
* 点击小图变大图 先隐藏小图 再生成大图显示
* @param {$this,maxImage} 当前点击的元素 当前大图
*/
_imgTool: function($this,maxImage) {
var self = this,
cfg = self.options;
var width = 0,
height = 0,
maxWidth = $this.closest(cfg.parentCls).outerWidth(); // 图片预先加载
var loadImg = function (url, fn) {
var img = new Image();
img.src = url;
if (img.complete) {
fn.call(img);
} else {
img.onload = function () {
fn.call(img);
};
};
};
loadImg(maxImage, function () {
width = this.width;
height = this.height;
tool();
});
function tool(){ var $thisParent = $($this).closest(cfg.parentCls);
// 当前图片隐藏掉
$this.hide();
if($('.artZoomBox',$thisParent).length > 0 && $('.artZoomBox',$thisParent).css('display') == "none"){
$('.artZoomBox',$thisParent).show();
}
// 图片等比例缩放
if (width > maxWidth) {
height = maxWidth / width * height;
width = maxWidth;
};
// 页面只创建一次
if($('.artZoomBox',$thisParent).length <= 0) {
var html = '<div class="artZoomBox">'+
'<div class="tool">'+
'<a class="hideImg" href="#" title="收起">收起</a>'+
'<a class="imgRight" href="#" title="向右转">向右转</a>'+
'<a class="imgLeft" href="#" title="向左转">向左转</a>'+
'<a class="viewImg" href="' + maxImage + '" title="查看原图">查看原图</a>'+
'</div>'+
'<a href="'+maxImage+'" class="maxImgLink">'+
'<img class="maxImg" width="'+width+'" height="'+height+'" src="'+maxImage+'"/>'+
'</a>'+
'</div>';
$($thisParent).append(html);
} $('.artZoomBox',$thisParent).find('a').unbind('click').bind('click',function(e){
e.preventDefault();
var $this = $(this),
$parent = $(this).closest(cfg.parentCls);
var box = $('.artZoomBox',$parent);
// 收起
if($($this).hasClass('hideImg') || $($this).hasClass('maxImgLink')) {
box.hide();
box.prev().show();
self.destory($this);
};
// 左旋转
if($($this).hasClass('imgLeft')) {
var target = box.find('.maxImg');
self._rotate(target,'left', width)
};
// 右旋转
if($($this).hasClass('imgRight')) {
var target = box.find('.maxImg');
self._rotate(target,'right', width);
};
// 新窗口打开
if($this.hasClass('viewImg')){
window.open(maxImage);
}
}); }
},
/*
* 图片旋转
* @param {target,direction,width} 要旋转的元素,方向, 旋转元素的宽度
*/
_rotate: function(target,direction,width){
var self = this,
cache = self.cache; var img = $(target)[0],
step = img.getAttribute('step');
if(img.length <= 0) {
return;
}
if (step == null) {
step = cache.minStep;
}
var width = img.width,
height = img.height;
if(direction == 'left') {
step--;
if(step < cache.minStep) {
step = cache.maxStep;
}
}else if(direction == 'right') {
step++;
if(step > cache.maxStep) {
step = cache.minStep;
}
}
img.setAttribute('step', step);
// IE
if(navigator.userAgent.indexOf('MSIE') >= 0) {
var s = $(img).attr('step');
img.style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(rotation=' + s + ')';
img.width = width;
img.height = height;
}else { // 对于现代浏览器 使用canvas
var canvas = $(img).next('canvas')[0];
if ($(img).next('canvas').length == 0) {
img.style.display = 'none';
canvas = document.createElement('canvas');
canvas.setAttribute('class', 'canvas');
img.parentNode.appendChild(canvas);
}
//旋转角度以弧度值为参数
var degree = step * 90 * Math.PI / 180;
var ctx = canvas.getContext('2d');
switch (step) {
case 0:
canvas.width = width;
canvas.height = height;
ctx.drawImage(img, 0, 0);
break;
case 1:
canvas.width = height;
canvas.height = width;
ctx.rotate(degree);
ctx.drawImage(img, 0, -height);
break;
case 2:
canvas.width = width;
canvas.height = height;
ctx.rotate(degree);
ctx.drawImage(img, -width, -height);
break;
case 3:
canvas.width = height;
canvas.height = width;
ctx.rotate(degree);
ctx.drawImage(img, -width, 0);
break;
}
}
$(target).attr("step",cache.step);
self.callback && $.isFunction(self.callback) && self.callback(cache.step);
},
/*
* 销毁
*/
destory: function($this){
var self = this,
cfg = self.options;
var curParent = $this.closest(cfg.parentCls),
canvas = $('.canvas',curParent),
maxImg = $('.maxImg',curParent); if(navigator.userAgent.indexOf('MSIE') >= 0) {
// IE
$(maxImg)[0].style.filter = 'progid:DXImageTransform.Microsoft.BasicImage(rotation=0)';
$(maxImg).attr('step',0);
}else{
if(canvas.length > 0) {
canvas.remove();
maxImg.show();
$(maxImg).attr("step",0);
}
}
}
});
var defaults = {
targetCls : '.artZoom',
parentCls : '.parentCls' // 当前元素最近的父元素的class
};

demo下载

HTML代码如下:

<li class="parentCls">
<a class="artZoom" href="http://m1.img.srcdd.com/farm5/d/2014/0718/21/EDEF32A674C1217FB6F80851514C124E_B250_400_250_278.jpeg">
<img src="http://m1.img.srcdd.com/farm4/d/2014/0718/21/31789C10D628913054C9B7F3A11D3519_B250_400_150_108.jpeg" /></a> </li>

其中父类 增加配置元素 parentCls ,当前大图链接写在href属性里面。

javascript仿新浪微博图片放大缩小及旋转效果的更多相关文章

  1. 鼠标滚轮图片放大缩小功能,使用layer弹框后不起作用

    今天在项目中遇到的一个问题:点击按钮使用layer弹框弹出一张图片,需要加一个鼠标滚轮放大缩小,图片也跟着放大缩小的功能.于是在网上找了一个demo. DEMO: <!DOCTYPE html ...

  2. hammer使用: 代码:捏合、捏开、图片放大 的一个手机图片“放大缩小可拖动”的小效果

    hammer.js 的使用. (手机手势插件) 捏合.捏开.图片放大 的一个手机图片“放大缩小可拖动”的小效果. 相关js 到 http://www.bootcdn.cn/  查找和下载. hamme ...

  3. jQuery练手:仿新浪微博图片文字列表淡进淡出上下滚动效果

    1.效果及功能说明 仿新浪微博图片文字列表上下淡进淡出间歇上下滚动 2.实现原理 首先要设定div内只能显示4个图片那么多出来的图片会自动隐藏然后在给图片添加一个动画的事件让他们可以滚动的播放出来上下 ...

  4. vue项目 一行js代码搞定点击图片放大缩小

    一行js代码搞定xue项目需要点击图片放大缩小,其实主要用的是用到了vue:class的动态切换,内容比较简单.一开始我把维护的需求想得太复杂了,和测试小姐姐聊了一下才反应过来. 两个月不到跟了四个项 ...

  5. 自定义mousewheel事件,实现图片放大缩小功能实现

    本文是承接 上一篇的<自定义鼠标滚动事件>  的基础上实现的,建议大家先看一下上一篇的mousewheel的实现,再浏览下文: 上篇中我们介绍到: $element.mousewheel( ...

  6. imageView图片放大缩小及旋转

    imageView图片放大缩小及旋转 一.简介 二.方法 1)设置图片放大缩小效果 第一步:将<ImageView>标签中的android:scaleType设置为"fitCen ...

  7. wpf下的图片放大缩小

    WPF下实现图片的放大缩小移动   在windows 7里面有自带的图片查看器,这个软件可以打开一张图片然后以鼠标在图片中的焦点为原点来进行缩放,并且放大后可以随意拖动.下面我们在WPF中实现这个功能 ...

  8. Canvas实现图片放大缩小移动操作

    对于HTML5相信大家都不陌生,很早就出来了,但是貌似都没有真正的使用过.最近做项目时要实现这样一个需求:一个图片,大小不固定,要求能实现类似地图一样放大.缩小.移动功能.这里就很合适使用html5的 ...

  9. 41.Android之图片放大缩小学习

    生活中经常会用到图片放大和缩小,今天简单学习下. 思路:1.添加一个操作图片放大和缩小类;  2. 布局文件中引用这个自定义控件;  3. 主Activity一些修改. 代码如下: 增加图片操作类: ...

随机推荐

  1. Android - 序列化与反序列化

    http://www.cnblogs.com/yezhennan/p/5527506.html http://blog.csdn.net/wangchunlei123/article/details/ ...

  2. java.lang.NoSuchMethodError: No static method getFont(Landroid/content/Context;ILandroid/util/TypedValue;ILandroid/widget/TextView;)

    global.gradle版本配置文件 原配置 compile_sdk_version = 26 build_tools_version = '26.0.2' target_sdk_version = ...

  3. 函数表达式(JavaScript高程笔记)

    函数声明 特点:函数声明提升(执行代码之前解析器会先读取函数声明,并使其在执行任何代码之前可用,意味着可以把函数声明放在调用语句之后) function functionName(arg0,arg1) ...

  4. 随手记:tomcat 与JDK 安装与配置

    写了3年的JAVA 每次遇到配置JDK 与按照tomcat的时候都要去网上找一下,太丢人了,所以还是记一下比较好,虽然都知道要配置哪些,但每次都还是有些不确定的感觉~ JDK : 1.安装官网 htt ...

  5. 【js实例】Array类型的9个数组方法,Date类型的41个日期方法,Function类型

    前文提要:[js实例]js中的5种基本数据类型和9种操作符 Array类型的9个数组方法 Array中有9个数组方法: 1.检测数组 2.转换方法 3.栈方法 4.队列方法 5.冲排序方法6.操作方法 ...

  6. 使用Picasso将加载的图片变成圆形

    http://blog.it985.com/14794.html,感谢该作者 Picasso的GITHUB地址:https://github.com/square/picasso. 怎么实现各种各样的 ...

  7. Flutter自定义布局套路

    开始 在Android中我们要实现一个布局需要继承ViewGroup, 重写其中的onLayout和onMeasure方法. 其中onLayout负责给子控件设置布局区域, onMeaseure度量子 ...

  8. VMware 15 安装 MAC OS 10.13 原版(详细图文教程)

    VMware 15 安装 MAC OS 10.13 原版(详细图文教程) 生命在于折腾,之前本想装个双系统黑苹果,什么 U 盘启动盘,四叶草引导,都配置好了,最后跪在一个动态卷上,备份格盘现在弄不了, ...

  9. CSS 小结笔记之em

    1.为什么使用em em也是css中的一种单位,和px类似.很多人会疑惑为什么有了px之后还要使用em,而且em使用起来相对于px来讲比较麻烦. em主要是应用于弹性布局,下面给出一个小栗子说明em的 ...

  10. Apache2启动错误Could not reliably determine the server's fully qualified domain name

    错误情况: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using ...