Angular中封裝fancyBox(圖片預(yù)覽)遇到問題小結(jié)
首先在官網(wǎng)下載最新版的fancyBox(一定要去最新網(wǎng)站,以前依賴的jquery版本偏低),附上鏈接:
http://fancyapps.com/fancybox/3/
然后在項目中引用jquery,然后在引用jquery.fancybox.min.css和jquery.fancybox.min.js。
如果需要動畫和鼠標(biāo)滾輪滾動效果還可以引入他提供的相關(guān)工具文件。
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內(nèi)容到這里 - > <! - 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添加自動化打包壓縮任務(wù),放在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 data-fancybox="images" data-width="2048" data-height="1365"> <img src="https://c1.staticflickr.com/9/8387/29155724700_58c1cb71cf_m.jpg" /> </a> <a data-fancybox="images" data-width="2048" data-height="1366"> <img src="https://c1.staticflickr.com/9/8148/29324593462_f890687b7a_m.jpg" /> </a> <a data-fancybox="images" data-width="2048" data-height="1365"> <img src="https://c1.staticflickr.com/9/8487/28808645394_9c7e6bf8a5_m.jpg" /> </a>
標(biāo)注:data-fancybox使用圖片預(yù)覽插件,三個值都為images表示在一個圖片組內(nèi) data-width data-height 圖像的真實寬高度 data-caption 標(biāo)題信息
啟用方法:
<script type="text/javascript"> $("[data-fancybox]").fancybox({ // Options will go here }); </script>
遇到的問題:
1.如果使用低版本的圖片預(yù)覽插件,回報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上的資源圖片,可以使用。
總結(jié)
以上所述是小編給大家介紹的Angular中封裝fancyBox(圖片預(yù)覽)遇到問題小結(jié),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
AngularJS監(jiān)聽ng-repeat渲染完成的方法
這篇文章主要介紹了AngularJS監(jiān)聽ng-repeat渲染完成的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03angularjs創(chuàng)建彈出框?qū)崿F(xiàn)拖動效果
這篇文章主要為大家詳細(xì)介紹了angularjs創(chuàng)建彈出框?qū)崿F(xiàn)拖動效果的相關(guān)資料,angularjs modal模態(tài)框創(chuàng)建可拖動的指令,感興趣的小伙伴們可以參考一下2016-01-01Angular懶加載動態(tài)創(chuàng)建顯示該模塊下聲明的組件
這篇文章主要為大家介紹了Angular懶加載動態(tài)創(chuàng)建顯示該模塊下聲明的組件,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05詳解如何構(gòu)建Angular項目目錄結(jié)構(gòu)
本篇文章主要介紹了詳解如何構(gòu)建Angular項目目錄結(jié)構(gòu),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07