angular中封装fancyBox(图片预览)

首先在官网下载最新版的fancyBox(一定要去最新网站,以前依赖的jquery版本偏低),附上链接:http://fancyapps.com/fancybox/3/

然后在项目中引用jquery,然后在引用jquery.fancybox.min.css和jquery.fancybox.min.js。

如果需要动画和鼠标滚轮滚动效果还可以引入他提供的相关工具文件。

1.你可以通过链接.css.js在你的html文件来安装fancyBox 。确保您也加载了jQuery库。以下是用作示例的基本HTML模板

<!DOCTYPE html><HTML>

<HEAD>

	<meta charset =“utf-8”>
	<title>我的页面</ title>

	<! -  CSS  - >
	<link rel =“stylesheet”type =“text / css”href =“jquery.fancybox.min.css”>
</ HEAD>
<BODY>

	<! - 您的HTML内容到这里 - >

	<! -  JS  - >
	<script src =“// code.jquery.com/jquery-3.2.1.min.js”> </ script>
	<script src =“jquery.fancybox.min.js”> </ script>
</ BODY>
</ HTML>

2.通过通过Bower或npm安装工具安装
# Bower
bower install fancybox --save

# NPM
npm install @fancyapps/fancybox --save

3.项目中通过外部引用,一般放在lib文件夹下(我采用的是这种方法)在lib下新建一个文件目录fancy文件夹,然后引入下载好的.js和.css,在gulpfile.js添加自动化打包压缩任务,放在css目录中的lib.min.css和lib.min.js,在入口index.html中引入压缩后的文件。以本fancyBox插件举例:

gulp.task(‘build-lib-js‘, [‘build-clean-third-lib-js‘], function () {
  var thirdLibJs = gulp.src([

  //外部引用js
  ‘./lib/fancybox/jquery.fancybox.min.js‘,
  ])
  .pipe(uglify())
  .pipe(concat(‘lib.min.js‘, {newLine: ‘\r\n‘}))
  .pipe(gulp.dest(‘js‘));

  return merge.apply(null, thirdLibJs);
  });

gulp.task(‘build-lib-css‘, [‘build-clean-lib-css‘], function () {    var thirdLibCss = gulp.src([      //外部引用css        ‘./lib/fancybox/jquery.fancybox.min.css‘    ])        .pipe(concat(‘lib.min.css‘, {newLine: ‘\r\n‘})) //放在哪个文件中        .pipe(gulp.dest(‘css‘));//打包输出目录(在哪个目录下)

return merge.apply(null, thirdLibCss);});

封装在angular自定义组件中html模块:
<img-box img-url="‘xxxxxx.png‘" img-style="‘width:740px;margin-left:-50px;‘"></img-box>

directive.js模块:
var appModule = angular.module(‘app.core‘);
appModule.directive(‘imgBox‘,imgBox);
function imgBox() {    return {        restrict:‘AE‘,        transclude:true,        scope:{            imgUrl:"=",            imgStyle:‘=‘        },        template:‘<a class="imageBox" href="{{imgUrl}}" data-fancybox><img style="{{imgStyle}}" src="{{imgUrl}}" th:src="${cdn.url(‘+"‘{{imgUrl}}‘"+‘)}" /></a>‘,        link:function (scope,elem,attrs) {            $(".imageBox").fancybox();        },    }}

官方写法:

  <a href="https://c1.staticflickr.com/9/8387/29155724700_a227577206_k.jpg" data-fancybox="images" data-width="2048" data-height="1365">
    <img src="https://c1.staticflickr.com/9/8387/29155724700_58c1cb71cf_m.jpg" />
  </a>

  <a href="https://c1.staticflickr.com/9/8148/29324593462_abebaddc38_k.jpg" data-fancybox="images" data-width="2048" data-height="1366">
    <img src="https://c1.staticflickr.com/9/8148/29324593462_f890687b7a_m.jpg" />
  </a>

  <a href="https://c1.staticflickr.com/9/8487/28808645394_a0ff0fc5c1_k.jpg" data-fancybox="images" data-width="2048" data-height="1365">
    <img src="https://c1.staticflickr.com/9/8487/28808645394_9c7e6bf8a5_m.jpg" />
  </a>

  标注:data-fancybox使用图片预览插件,三个值都为images表示在一个图片组内 data-width data-height 图像的真实宽高度 data-caption 标题信息

  启用方法:

  <script type="text/javascript">
	$("[data-fancybox]").fancybox({
		// Options will go here
	});
  </script>

  遇到的问题:  1.如果使用低版本的图片预览插件,回报Cannot read property ‘msie‘ of undefined的错,原因低版本似乎使用$ .browser方法,但是从jQuery 1.9起已被删除  2.在template或者templateUrl要使用html中传入的imgUrl值,不能直接使用imgUrl或者scope.imgUrl获取。  方法:template:‘<a class="imageBox" href="{{imgUrl}}" data-fancybox><img style="{{imgStyle}}" src="{{imgUrl}}" th:src="${cdn.url(‘+"‘{{imgUrl}}‘"+‘)}" /></a>‘      或者     template:‘<a class="imageBox" ng-href="{{imgUrl}}" data-fancybox><img style="{{imgStyle}}" ng-src="{{imgUrl}}" th:src="${cdn.url(‘+"‘{{imgUrl}}‘"+‘)}" /></a>‘     后面的th:src可以不用拼接,如果你项目中是用cdn上的资源图片,可以使用。
 

  


 
时间: 2024-08-30 13:48:37

angular中封装fancyBox(图片预览)的相关文章

vue项目中图片预览旋转功能

最近项目中需要在图片预览时,可以旋转图片预览,在网上找了下,发现有一款功能强大的图片组件:viewerjs. git-hup: https://github.com/fengyuanchen/viewerjs 在git上看了下,有很多功能,不过我的项目只需要做个图片旋转功能,引入这个组件感觉大材小用了,于是自己写了个简易版的,因为我们只是查看而已,没什么要求.如果你需要比较精确的图片旋转功能,可以使用这个viewerjs组件 功能描述: 一个图片预览框,三个操作按钮: 上一张,下一张,旋转; 点

纯前端的图片预览

尊重原创,转载请注明来自:http://www.cnblogs.com/fsjohnhuang/p/3925827.html ^_^肥仔John 一.前言 图片上传是一个普通不过的功能,而图片预览就是就是上传功能中必不可少的子功能了.在这之前,我曾经通过订阅input[type=file]元素的onchange事件,一旦更改路径则将图片上传至服务器,接着就获取图片路径并赋值到img元素上.先不管文件异步提交的解决方案,就是服务端清理那些临时的预览图片已经增加不少工作量了. 偶然从MDN上找到纯前

dwz+jquery+fileupload+springmvc实现文件上传 及图片预览

1 前台jsp:文件的上传利用了iframe实现局部刷新功能.使用了apache的fileupload组件,用到的jar: commons-fileupload.jar,commons-io.jarDiskFileItemFactory fac = new DiskFileItemFactory();ServletFileUpload upload = new ServletFileUpload(fac);upload.setHeaderEncoding("utf-8"); Html代

JS魔法堂之实战:纯前端的图片预览

一.前言 图片上传是一个普通不过的功能,而图片预览就是就是上传功能中必不可少的子功能了.在这之前,我曾经通过订阅input[type=file]元素的onchange事件,一旦更改路径则将图片上传至服务器,接着就获取图片路径并赋值到img元素上.先不管文件异步提交的解决方案,就是服务端清理那些临时的预览图片已经增加不少工作量了. 偶然从MDN上找到纯前端图片预览的相关资料,经过整理后记录下来以便日后查阅. 二.准备功夫1──FileReader FileReader是HTML5的新特性,用于读取

android 图片选择器 图片预览

需求:近段时间公司有要求写一个类似于微信发送图片时,用来选择照片的一个图片浏览器,本来想在网上找一个直接拿来用,找寻无果,只能自己写了.相信有很多网页也有这样的需求,这里我将写好的源码打包成library工程分享给大家!! 转载请注明出处:http://blog.csdn.net/a740169405/article/details/41622025 说明: ①本来打算自己写图片异步加载代码,后来因为赶时间,就改成直接引用开源框架universal-image-loader,一个        

js实现移动端图片预览:手势缩放, 手势拖动,双击放大...

.katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > .katex-html { display: block; } .katex-display > .katex > .katex-html > .tag { position: absolute; right: 0px; } .katex { font-style: normal; font

jquery实现上传图片及图片大小验证、图片预览效果代码

jquery实现上传图片及图片大小验证.图片预览效果代码 上传图片验证 */ function submit_upload_picture(){     var file = $('file_c').value;     if(!/.(gif|jpg|jpeg|png|gif|jpg|png)$/.test(file)){            alert("图片类型必须是.gif,jpeg,jpg,png中的一种")        }else{      $('both_form')

图片预览

下方点击小图标,上方显示大图标. 布局文件如下 <body> <h1>图片预览</h1> <p> <img id="largeImg" src="images/img1-lg.jpg" alt="Large Image" /> </p> <p class="thumbs"> <!-- 点击小图片 看到大图片 只是超连接的话 会新打开页面,

Uploadify—借助Uploadify插件实现图片预览时如何解决Chrome浏览器报“喔唷,崩溃啦”

今天借助Uploadify插件实现了图片预览及其上传的功能,可是在Chrome浏览器中会时不时地出现如下图所示的情况: 解决方法:使用这种方法引用该插件js文件: <script type="text/javascript"> document.write("<script type='text/javascript' src='<%=basePath %>js/uploadify/jquery.uploadify.min.js?" +