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

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

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

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

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

我的页面</ 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></pre> </div> <p>2.通过通过Bower或npm安装工具安装</p> <div class="jb51code"> <pre class="brush:js;"> # Bower bower install fancybox --save # NPM npm install @fancyapps/fancybox --save</pre> </div> <p>3.项目中通过外部引用,一般放在lib文件夹下(我采用的是这种方法)</p> <p>在lib下新建一个文件目录fancy文件夹,然后引入下载好的.js和.css,在gulpfile.js添加自动化打包压缩任务,放在css目录中的lib.min.css和lib.min.js,在入口index.html中引入压缩后的文件。</p> <p>以本fancyBox插件举例:</p> <div class="jb51code"> <pre class="brush:js;"> 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); });</pre> </div> <p>封装在angular自定义组件中</p> <p>html模块:</p> <div class="jb51code"> <pre class="brush:js;"> <img-box img-url="'xxxxxx.png'" img-style="'width:740px;margin-left:-50px;'"></img-box></pre> </div> <p>directive.js模块:</p> <div class="jb51code"> <pre class="brush:js;"> var appModule = angular.module('app.core'); appModule.directive('imgBox',imgBox);</pre> </div> <div class="jb51code"> <pre class="brush:js;"> 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 (s<a href="https://www.jb51.cc/tag/cop/" target="_blank" class="keywords">cop</a>e,elem,attrs) { $(".image<a href="https://www.jb51.cc/tag/Box/" target="_blank" class="keywords">Box</a>").<a href="https://www.jb51.cc/tag/fancybox/" target="_blank" class="keywords">fancybox</a>(); },} }</pre> </div> <p>官方写法:</p> <div class="jb51code"> <pre class="b<a href="https://www.jb51.cc/tag/rush/" target="_blank" class="keywords">rush</a>:js;"> <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">     <p class="pic_center"><img src="https://c1.staticflickr.com/9/8148/29324593462_f890687b7a_m.jpg" /></p>   </a>   <a href="https://c1.staticflickr.com/9/8487/28808645394_a0ff0fc5c1_k.jpg" data-fancybox="images" data-width="2048" data-height="1365">     <p class="pic_center"><img src="https://c1.staticflickr.com/9/8487/28808645394_9c7e6bf8a5_m.jpg" /></p>   </a></pre> </div> <p>  标注:data-<a href="https://www.jb51.cc/tag/fancybox/" target="_blank" class="keywords">fancybox</a>使用<a href="https://www.jb51.cc/tag/tupian/" target="_blank" class="keywords">图片</a>预览<a href="https://www.jb51.cc/tag/chajian/" target="_blank" class="keywords">插件</a>,三个值都为images表示在<a href="https://www.jb51.cc/tag/yige/" target="_blank" class="keywords">一个</a><a href="https://www.jb51.cc/tag/tupian/" target="_blank" class="keywords">图片</a>组内 data-width data-height 图像的真实宽高度 data-caption <a href="https://www.jb51.cc/tag/biaoti/" target="_blank" class="keywords">标题</a>信息</p> <p>  启用<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>: </p> <div class="jb51code"> <pre class="b<a href="https://www.jb51.cc/tag/rush/" target="_blank" class="keywords">rush</a>:js;"> <script type="text/javascript"> $("[data-<a href="https://www.jb51.cc/tag/fancybox/" target="_blank" class="keywords">fancybox</a>]").<a href="https://www.jb51.cc/tag/fancybox/" target="_blank" class="keywords">fancybox</a>({ // Options will go here });   </script></pre> </div> <p>  遇到的问题:</p> <p>  1.如果使用低版本的<a href="https://www.jb51.cc/tag/tupian/" target="_blank" class="keywords">图片</a>预览<a href="https://www.jb51.cc/tag/chajian/" target="_blank" class="keywords">插件</a>,回报Cannot read property 'msie' of undefined的错,原因低版本似乎使用$ .<a href="https://www.jb51.cc/tag/bro/" target="_blank" class="keywords">bro</a>wser<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>,但是从jQuery 1.9起已被<a href="https://www.jb51.cc/tag/shanchu/" target="_blank" class="keywords">删除</a></p> <p>  2.在template或者templateUrl要使用html中传入的<a href="https://www.jb51.cc/tag/imgur/" target="_blank" class="keywords">imgur</a>l值,不能直接使用<a href="https://www.jb51.cc/tag/imgur/" target="_blank" class="keywords">imgur</a>l或者s<a href="https://www.jb51.cc/tag/cop/" target="_blank" class="keywords">cop</a>e.<a href="https://www.jb51.cc/tag/imgur/" target="_blank" class="keywords">imgur</a>l<a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>。</p> <p>  <a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>:</p> <div class="jb51code"> <pre class="b<a href="https://www.jb51.cc/tag/rush/" target="_blank" class="keywords">rush</a>:js;"> 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>'</pre> </div> <p>      或者</p> <div class="jb51code"> <pre class="b<a href="https://www.jb51.cc/tag/rush/" target="_blank" class="keywords">rush</a>:js;"> 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>'</pre> </div> <p>     后面的th:src可以不用拼接,如果你项目中是用cdn上的资源<a href="https://www.jb51.cc/tag/tupian/" target="_blank" class="keywords">图片</a>,可以使用。 </p> <p><h3>总结</h3></p> <p>以上所述是小编给大家介绍的Angular中封装<a href="https://www.jb51.cc/tag/fancybox/" target="_blank" class="keywords">fancybox</a>(<a href="https://www.jb51.cc/tag/tupian/" target="_blank" class="keywords">图片</a>预览)遇到问题小结。编程之家 jb51.cc 收集整理的教程希望能对你有所帮助,如果觉得编程之家不错,可<a href="https://www.jb51.cc/tag/fenxiang/" target="_blank" class="keywords">分享</a>给好友!感谢<a href="https://www.jb51.cc/tag/zhichi/" target="_blank" class="keywords">支持</a>。</p><div class="topcard-tags"><a href="https://m.jb51.cc/tag/angular/" class="tag_link" target="_blank">angular</a><a href="https://m.jb51.cc/tag/angularp/" class="tag_link" target="_blank">angular</a><a href="https://m.jb51.cc/tag/tupianyulan/" class="tag_link" target="_blank">图片预览</a></div> </div> </div> </div> <!-- row end--> <div class="clearfix"></div> <!-- row --> <div class="col-sm-12 col-md-12 col-lg-12"> <div class="card"> <div class="title"><h1>相关文章</h1></div><div class="list_con"> <img src="https://m.jb51.cc/res/2024/07-30/10/71a2262aa7e5c72cb07d2f3e32b1d2a2.png" width="100" height="64" style="float:right;margin-left:20px;" /> <div class="title"> <a href="https://m.jb51.cc/js/4739037.html" title="【JavaScript】聊一聊js中的浅拷贝与深拷贝与手写实现">【JavaScript】聊一聊js中的浅拷贝与深拷贝与手写实现</a> </div> <div class="summary">什么是深拷贝与浅拷贝?深拷贝与浅拷贝是js中处理对象或数据...</div> </div><div class="list_con"> <div class="title"> <a href="https://m.jb51.cc/js/4739036.html" title="【JavaScript】前端算法题(重建二叉树、反向输出链表每个节点)">【JavaScript】前端算法题(重建二叉树、反向输出链表每个节点)</a> </div> <div class="summary">前言 今天复习了一些前端算法题,写到一两道比较有意思的题:...</div> </div><div class="list_con"> <img src="https://m.jb51.cc/res/2024/07-30/10/722d62c6b119ef7c0f54f9c0c68c2c56.jpg" width="100" height="64" style="float:right;margin-left:20px;" /> <div class="title"> <a href="https://m.jb51.cc/js/4739035.html" title="【JavaScript】聊聊js中关于this的指向">【JavaScript】聊聊js中关于this的指向</a> </div> <div class="summary">最近在看回JavaScript的面试题,this 指向问题是入坑前端必须...</div> </div><div class="list_con"> <img src="https://m.jb51.cc/res/2022/01-13/18/1662e1e23a972adfd4c7eb13ed97b0b9.gif" width="100" height="64" style="float:right;margin-left:20px;" /> <div class="title"> <a href="https://m.jb51.cc/js/4675608.html" title="js如何实现弹出form提交表单?">js如何实现弹出form提交表单?</a> </div> <div class="summary">js如何实现弹出form提交表单?(图文+视频)</div> </div><div class="list_con"> <img src="https://m.jb51.cc/res/2022/01-13/18/aefa150d17138168dd5f72e5326e0b21.png" width="100" height="64" style="float:right;margin-left:20px;" /> <div class="title"> <a href="https://m.jb51.cc/js/4675588.html" title="js怎么获取复选框选中的值">js怎么获取复选框选中的值</a> </div> <div class="summary">js怎么获取复选框选中的值</div> </div><div class="list_con"> <img src="https://m.jb51.cc/res/2022/01-13/18/0699e4dc91b71c1ec0d5eee3c437ab6d.gif" width="100" height="64" style="float:right;margin-left:20px;" /> <div class="title"> <a href="https://m.jb51.cc/js/4675585.html" title="js如何实现倒计时跳转页面">js如何实现倒计时跳转页面</a> </div> <div class="summary">js如何实现倒计时跳转页面</div> </div></div> </div> <!-- row end--> </div> </div> <footer id="footer"> <div class="container"> <div class="copyright"> Copyright © 2018 编程之家. 当前版本 V7.0.16<br> <span class="ml5">编程之家 版权所有 <a href="https://beian.miit.gov.cn/" target="_blank" rel="nofollow">闽ICP备13020303号-8</a> </div> </div> </footer> <script> (function () { var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(bp, s); })(); </script> <script src="https://m.jb51.cc/js/count.js"></script> </body> </html>