Angular中封装fancyBox(图片预览)遇到问题小结


Posted in Javascript onSeptember 01, 2017

首先在官网下载最新版的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}}" rel="external nofollow" rel="external nofollow" rel="external nofollow" 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}}" rel="external nofollow" rel="external nofollow" rel="external nofollow" data-fancybox><img style="{{imgStyle}}" src="{{imgUrl}}" th:src="${cdn.url('+"'{{imgUrl}}'"+')}" /></a>'

或者

template:'<a class="imageBox" ng-href="{{imgUrl}}" rel="external nofollow" rel="external nofollow" rel="external nofollow" data-fancybox><img style="{{imgStyle}}" ng-src="{{imgUrl}}" th:src="${cdn.url('+"'{{imgUrl}}'"+')}" /></a>'

 后面的th:src可以不用拼接,如果你项目中是用cdn上的资源图片,可以使用。

总结

以上所述是小编给大家介绍的Angular中封装fancyBox(图片预览)遇到问题小结,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Javascript 相关文章推荐
javascript控制在光标位置插入文字适合表情的插入
Jun 09 Javascript
js和jquery设置disabled属性为true使按钮失效
Aug 07 Javascript
javascript 中的 delete及delete运算符
Nov 15 Javascript
js生成随机数的过程解析
Nov 24 Javascript
jQuery mobile类库使用时加载导航历史的方法简介
Dec 04 Javascript
js删除Array数组中指定元素的两种方法
Aug 03 Javascript
AngularJS 支付倒计时功能实现思路
Jun 05 Javascript
Nuxt.js实战详解
Jan 18 Javascript
jQuery实现的简单对话框拖动功能示例
Jun 05 jQuery
解决循环中setTimeout执行顺序的问题
Jun 20 Javascript
微信小程序开发问题之wx.previewImage
Dec 25 Javascript
原生js+css实现tab切换功能
Sep 17 Javascript
BootStrap模态框和select2合用时input无法获取焦点的解决方法
Sep 01 #Javascript
Angularjs实现上传图片预览功能
Sep 01 #Javascript
JavaScript中使用参数个数实现重载功能
Sep 01 #Javascript
bootstrap-table实现服务器分页的示例 (spring 后台)
Sep 01 #Javascript
jQuery EasyUI结合zTree树形结构制作web页面
Sep 01 #jQuery
基于Vue.js 2.0实现百度搜索框效果
Dec 28 #Javascript
JavaScript闭包的简单应用
Sep 01 #Javascript
You might like
PHP新手用的Insert和Update语句构造类
2012/03/31 PHP
解析php中static,const与define的使用区别
2013/06/18 PHP
php面象对象数据库操作类实例
2014/12/02 PHP
详解PHP中的null合并运算符
2015/12/30 PHP
详解php语言最牛掰的Laravel框架
2017/11/20 PHP
使用JQUERY进行后台页面布局控制DIV实现左右式
2014/01/07 Javascript
浅析jQuery中调用ajax方法时在不同浏览器中遇到的问题
2014/06/11 Javascript
Javascript基础教程之数据类型 (字符串 String)
2015/01/18 Javascript
jquery实现textarea 高度自适应
2015/03/11 Javascript
JS动态修改iframe内嵌网页地址的方法
2015/04/01 Javascript
jQuery实现购物车表单自动结算效果实例
2015/08/10 Javascript
简介BootStrap model弹出框的使用
2016/04/27 Javascript
Node.js中如何合并两个复杂对象详解
2016/12/31 Javascript
在点击div中的p时,如何阻止事件冒泡
2017/02/07 Javascript
Node.js常用工具之util模块
2017/03/09 Javascript
JS组件系列之Gojs组件 前端图形化插件之利器
2017/11/29 Javascript
通过封装scroll.js 获取滚动条的值
2018/07/13 Javascript
Vue+element+cookie记住密码功能的简单实现方法
2020/09/20 Javascript
解决新建一个vue项目过程中遇到的问题
2020/10/22 Javascript
jquery实现加载更多&quot;转圈圈&quot;效果(示例代码)
2020/11/09 jQuery
Python实现的朴素贝叶斯分类器示例
2018/01/06 Python
Django高级编程之自定义Field实现多语言
2019/07/02 Python
Python Selenium 之数据驱动测试的实现
2019/08/01 Python
python怎么提高计算速度
2020/06/11 Python
Django+Django-Celery+Celery的整合实战
2021/01/20 Python
智能电子秤、手表和健康监测仪:Withings(之前为诺基亚健康)
2018/10/30 全球购物
斯巴达比赛商店:Spartan Race
2019/01/08 全球购物
县优秀教师事迹材料
2014/01/31 职场文书
个人担保书格式范文
2014/05/12 职场文书
上班迟到检讨书300字
2014/10/18 职场文书
2014年消防工作总结
2014/11/21 职场文书
工厂门卫岗位职责
2015/04/13 职场文书
风之谷观后感
2015/06/11 职场文书
matplotlib之pyplot模块实现添加子图subplot的使用
2021/04/25 Python
详解vue中v-for的key唯一性
2021/05/15 Vue.js
mysql 乱码 字符集latin1转UTF8
2022/04/19 MySQL