首先在官网下载最新版的fancybox(一定要去最新网站,以前依赖的jquery版本偏低),附上链接:
然后在项目中引用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” </ 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" 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="http://www.51sjk.com/Upload/Articles/1/0/288/288987_20210728110317307.jpg" /> </a> <a data-fancybox="images" data-width="2048" data-height="1366"> <img src="http://www.51sjk.com/Upload/Articles/1/0/288/288987_2021072811031730701.jpg" /> </a> <a data-fancybox="images" data-width="2048" data-height="1365"> <img src="http://www.51sjk.com/Upload/Articles/1/0/288/288987_20210728110317323.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" 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- 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(图片预览)遇到问题小结,希望对大家有所帮助
哈69338294