日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

angular中封装fancyBox(图片预览)

發布時間:2023/12/13 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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上的資源圖片,可以使用。 ?

?

  -----原創文章,?版權所有,轉載請注明標明出處:http://www.cnblogs.com/doinbean

  



?

轉載于:https://www.cnblogs.com/doinbean/p/7459838.html

總結

以上是生活随笔為你收集整理的angular中封装fancyBox(图片预览)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。