Angular中封装fancyBox(图片预览)遇到问题小结-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

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

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

成都创新互联公司专注于企业营销型网站、网站重做改版、建湖网站定制设计、自适应品牌网站建设、H5场景定制成都商城网站开发、集团公司官网建设、外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为建湖等各大城市提供网站开发制作服务。

http://fancyapps.com/fancybox/3/

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

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

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

<!DOCTYPE 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><pre># 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><pre>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><pre><img-box img-url="'xxxxxx.png'" img-></img-box></pre></div><p>directive.js模块:</p><div><pre>var appModule = angular.module('app.core');
appModule.directive('imgBox',imgBox);</pre></div><div><pre>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  src="{{imgUrl}}" th:src="${cdn.url('+"'{{imgUrl}}'"+')}" /></a>',
    link:function (scope,elem,attrs) {
      $(".imageBox").fancybox();
    },
  }
}</pre></div><p>官方写法:</p><div><pre><a href="/upload/otherpic56/66509.jpg" data-fancybox="images" data-width="2048" data-height="1365">
    <img src="/upload/otherpic56/66510.jpg" />
  </a>
  <a href="/upload/otherpic56/66511.jpg" data-fancybox="images" data-width="2048" data-height="1366">
    <img src="/upload/otherpic56/66513.jpg" />
  </a>
  <a href="/upload/otherpic56/66527.jpg" data-fancybox="images" data-width="2048" data-height="1365">
    <img src="/upload/otherpic56/66539.jpg" />
  </a></pre></div><p>标注:data-fancybox使用图片预览插件,三个值都为images表示在一个图片组内 data-width data-height 图像的真实宽高度 data-caption 标题信息</p><p>启用方法: </p><div><pre><script type="text/javascript">
 $("[data-fancybox]").fancybox({
 // Options will go here
 });
  </script></pre></div><p>遇到的问题:</p><p>1.如果使用低版本的图片预览插件,回报Cannot read property 'msie' of undefined的错,原因低版本似乎使用$ .browser方法,但是从jQuery 1.9起已被删除</p><p>2.在template或者templateUrl要使用html中传入的imgUrl值,不能直接使用imgUrl或者scope.imgUrl获取。</p><p>方法:</p><div><pre>template:'<a class="imageBox" href="{{imgUrl}}" rel="external nofollow" rel="external nofollow" rel="external nofollow" data-fancybox><img  src="{{imgUrl}}" th:src="${cdn.url('+"'{{imgUrl}}'"+')}" /></a>'</pre></div><p>或者</p><div><pre>template:'<a class="imageBox" ng-href="{{imgUrl}}" rel="external nofollow" rel="external nofollow" rel="external nofollow" data-fancybox><img  ng-src="{{imgUrl}}" th:src="${cdn.url('+"'{{imgUrl}}'"+')}" /></a>'</pre></div><p>后面的th:src可以不用拼接,如果你项目中是用cdn上的资源图片,可以使用。</p><p><strong>总结</strong></p><p>以上所述是小编给大家介绍的Angular中封装fancyBox(图片预览)遇到问题小结,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对创新互联网站的支持!</p>            
            
                        <br>
            本文名称:Angular中封装fancyBox(图片预览)遇到问题小结            <br>
            网页网址:<a href="http://kswsj.cn/article/gpegid.html">http://kswsj.cn/article/gpegid.html</a>
        </div>
    </div>
    <div class="other">
        <h3>其他资讯</h3>
        <ul>
            <li>
                    <a href="/article/dccjsjc.html">linux开启打印命令 linux开启打印服务</a>
                </li><li>
                    <a href="/article/dccjsgg.html">帝国cms仿QQ国际 帝国cms api</a>
                </li><li>
                    <a href="/article/dccjsgp.html">java程序代码基础 java的程序代码</a>
                </li><li>
                    <a href="/article/dccjspd.html">腾讯云服务器桌面是黑色的 腾讯云服务器桌面是黑色的吗</a>
                </li><li>
                    <a href="/article/dccjsjo.html">二叉树java代码现实 java实现二叉树的遍历</a>
                </li>        </ul>
    </div>
</div>
<div class="line"></div>
<!--底部-->
<footer id="5">
    <div class="foot1 container">
        <div class="list">
            <div class="item">
                <a href="javascript:;">
                    <span class="ico1"><i class="iconfont"></i><img src="/Public/Home/img/ewm.png" alt=""></span>
                    <strong>关注我们</strong>
                </a>
            </div>
            <div class="item">
                <a href="" target="_blank">
                    <span><i class="iconfont"></i></span>
                    <strong>索要报价</strong>
                </a>
            </div>
            <div class="item">
                <a href="" target="_blank">
                    <span><i class="iconfont"></i></span>
                    <strong>我要咨询</strong>
                </a>
            </div>
            <div class="item">
                <a href="" target="_blank">
                    <span><i class="iconfont"></i></span>
                    <strong>找到我们</strong>
                </a>
            </div>
            <div class="item">
                <a href="" target="_blank">
                    <span><i class="iconfont"></i></span>
                    <strong>投诉建议</strong>
                </a>
            </div>
        </div>
        <div class="tel">
            <dl>
                <tel><a href="tel:400-028-6601" target="_blank">400-028-6601</a></tel><br>
                <span>也许您需要专业的服务,欢迎来电咨询</span>
            </dl>
            <dl>
                <tel><a href="tel:18980820575" target="_blank">18980820575</a></tel><br>
                <span>您的需求,是我们前进的动力</span>
            </dl>
        </div>
    </div>
    <div class="friend">
        <div class="container">
            <span class="tit">友情链接:</span>
            <div class="inner">
                <a href="https://www.cdcxhl.com/ruanwen/" target="_blank">营销软文</a><a href="https://www.cdcxhl.com/douyin/" target="_blank">抖音视频拍摄</a><a href="https://www.cdcxhl.com/ruanwen/" target="_blank">发布软文</a><a href="https://www.cdcxhl.com/seo.html" target="_blank">成都网站推广</a><a href="https://www.cdcxhl.com/mobile.html" target="_blank">成都手机网站建设</a><a href="https://www.cdcxhl.com/tuoguan/" target="_blank">成都托管服务器</a><a href="https://www.cdcxhl.com/mobile.html" target="_blank">手机网站制作</a><a href="https://www.cdcxhl.com/jigui/" target="_blank">服务器托管机柜</a><a href="https://www.cdcxhl.com/xiangyingshi.html" target="_blank">成都响应式网站设计</a><a href="https://www.cdcxhl.com/gaofang/" target="_blank">高防服务器租用</a><a href="https://www.cdcxhl.com/quanwang.html" target="_blank">全网整合营销推广</a><a href="https://www.cdcxhl.com/wangzhandingzhi.html" target="_blank">成都定制网站</a><a href="https://www.cdxwcx.com/jifang/xiyun.html" target="_blank">西云移动机房</a><a href="https://www.cdcxhl.com/" target="_blank">成都网站设计</a><a href="https://www.cdcxhl.com/weihu/" target="_blank">成都网站维护公司</a><a href="https://www.cdcxhl.com/hangyead/" target="_blank">广告投放平台</a><a href="https://www.cdcxhl.com/" target="_blank">做网站</a><a href="https://www.cdcxhl.com/google.html" target="_blank">谷歌推广</a>            </div>
        </div>
    </div>
    <div class="foot">
        <div class="container">
            <div class="footNav">
                <h3>网站建设</h3>
                <a href="http://www.cdkjz.cn/wangzhan/waimao/" target="_blank">成都外贸网站建设</a><a href="http://m.cdcxhl.cn/seo/" target="_blank">成都网站建设推广</a><a href="http://www.scyingshan.cn/" target="_blank">营山网站建设</a>            </div>
            <div class="footNav">
                <h3>服务器托管</h3>
                <a href="https://www.cdcxhl.com/idc/cqst.html" target="_blank">重庆电信水土机房托管</a><a href="http://www.cdxwcx.cn/tuoguan/mianyang.html" target="_blank">绵阳主机托管</a><a href="https://www.cdcxhl.com/idc/xixin.html" target="_blank">西信服务器托管</a>            </div>
            <div class="footNav">
                <h3>网站制作</h3>
                <a href="http://www.myzitong.com/" target="_blank">梓潼网站制作公司</a><a href="http://www.cxhljz.cn/" target="_blank">成都网站制作</a><a href="http://m.cdcxhl.com/" target="_blank">成都网站制作</a>            </div>
            <div class="footNav">
                <h3>企业服务</h3>
                <a href="https://www.cdcxhl.com/ruanwen/" target="_blank">软文发布</a><a href="https://www.cdcxhl.com/service/service.html" target="_blank">工商服务</a><a href="https://www.cdcxhl.com/service/licence.html" target="_blank">药房许可证</a>            </div>
            <div class="fr ecode">
                <div class="fl">
                    <img src="/Public/Home/img/ewm.jpg">
                    <p>关注企业微信</p>
                </div>
                <div class="fr slogan">
                    <p class="icon">
                        <a class="ph" href=""><i class="iconfont"></i></a>
                        <a class="qq" href="tencent://message/?uin=1683211881&Site=&Menu=yes"><i class="iconfont"></i></a>
                    </p>
                    <p>
                        <i>想要找 </i> <a href="">小程序开发</a>、<a href="">APP开发</a>、
                        <a href="">营销型网站建设</a>、<a href="">网站建设</a>、
                        <i><a href="">网站定制开发</a></i> ,就选<a href="">创新互联</a>
                    </p>
                </div>
            </div>
        </div>
        <div class="bottom container">
            <p class="fl">
                版权所有:成都创新互联科技有限公司
                备案号:<a href="https://beian.miit.gov.cn/" target="_blank" rel="nofollow">蜀ICP备19037934号</a>
                服务热线:028-86922220
            </p>
            <p class="fr">
                <a href="https://www.cdxwcx.com/" target="_blank">成都网站建设</a>:
                <a href="https://www.cdcxhl.com/" target="_blank">创新互联</a>
            </p>
        </div>
    </div>
</footer>
<!--在线咨询-->
<div class="fot">
    <ul>
        <li>
            <a href="https://p.qiao.baidu.com/cps/mobileChat?siteId=11284691&userId=6256368&type=1&reqParam=%20{%22from%22:0,%22sessionid%22:%22%22,%22siteId%22:%2211284691%22,%22tid%22:%22-1%22,%22userId%22:%226256368%22,%22ttype%22:1,%22siteConfig%22:%20{%22eid%22:%226256368%22,%22queuing%22:%22%22,%22siteToken%22:%226ce441ff9e2d6bedbdfc2a4138de449e%22,%22userId%22:%226256368%22,%22isGray%22:%22false%22,%22wsUrl%22:%22wss://p.qiao.baidu.com/cps3/websocket%22,%22likeVersion%22:%22generic%22,%22siteId%22:%2211284691%22,%22online%22:%22true%22,%22webRoot%22:%22//p.qiao.baidu.com/cps3/%22,%22bid%22:%22160142915792139572%22,%22isSmallFlow%22:0,%22isPreonline%22:0,%22invited%22:0%20},%22config%22:%20{%22themeColor%22:%224d74fa%22%20}%20}&appId=&referer=&iswechat=0&expectWaiter=-1&openid=null&otherParam=null&telephone=null&speedLogId=null&eid=null&siteToken=6ce441ff9e2d6bedbdfc2a4138de449e" target="_blank">
                <img src="/Public/Home/img/fot1.png" alt="">
                <p>在线咨询</p>
            </a>
        </li>
        <li>
            <a href="tel:18980820575" target="_blank">
                <img src="/Public/Home/img/fot2.png" alt="">
                <p>拨打电话</p>
            </a>
        </li>
    </ul>
</div>
</body>
</html>
<script>
    $(".con img").each(function(){
        var src = $(this).attr("src");    //获取图片地址
        var str=new RegExp("http");
        var result=str.test(src);
        if(result==false){
            var url = "https://www.cdcxhl.com"+src;    //绝对路径
            $(this).attr("src",url);
        }
    });
    window.onload=function(){
        document.oncontextmenu=function(){
            return false;
        }
    }
</script>