首先在官網下載最新版的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>
template:'<a class="imageBox" href="{{imgUrl}}" data-fancybox><img style="{{imgStyle}}" src="{{imgUrl}}" th:src="${cdn.url('+"'{{imgUrl}}'"+')}" /></a>'
遇到的問題:
1.如果使用低版本的圖片預覽插件,會報Cannot read property 'msie' of undefined的錯,原因低版本似乎使用$ .browser方法,但是從jQuery 1.9起已被刪除
2.在template或者templateUrl要使用html中傳入的imgUrl值,不能直接使用imgUrl或者scope.imgUrl獲取。
方法:
或者
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
?