强大的jQuery焦点图插件Owl Carousel + 响应式图片(案例解析)

GitHub地址:https://github.com/OwlCarousel2/OwlCarousel2

在各式各样的网站中,都能看到焦点图插件的身影。

一个好的焦点图插件必须满足以下特点:1. 支持不同数量的图片 2. 支持响应式布局 3. 具有良好的兼容性

Owl Carousel完全满足这几个特点。Owl Carousel是非常强大的jQuery焦点图插件,这个焦点图插件功能众多,高度可定制性,支持触摸设备,并且是响应式的。

本文将通过一个自适应轮播图案例,介绍Owl Carousel结合响应式图片的使用方法。

GitHub地址:https://github.com/Hayley2016/OwlCarousel-with-responsiveImage

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>强大的jQuery焦点图插件Owl Carousel + 响应式图片(案例解析)</title>
    <link rel="stylesheet" href="node_modules/owl.carousel/dist/assets/owl.carousel.min.css" />
    <style>
        .ad .item {
            height: 200px;
        }
    </style>
</head>
<body>
<div class="container">
    <section class="ad">
        <div class="owl-carousel owl-theme">
            <div class="item">
                <picture>
                    <source srcset="img/ad001-l.png" media="(min-width:50em)">
                    <source srcset="img/ad001-m.png" media="(min-width:30em)">
                    <img src="img/ad001.png" alt="2015年度报告">
                </picture>
            </div>
            <div class="item">
                <picture>
                    <source srcset="img/ad002-l.png" media="(min-width: 50em)">
                    <source srcset="img/ad002-m.png" media="(min-width: 30em)">
                    <img srcset="img/ad002.png" alt="新年红包">
                </picture>
            </div>
            <div class="item">
                <picture>
                    <source srcset="img/ad003-l.png" media="(min-width: 50em)">
                    <source srcset="img/ad003-m.png" media="(min-width: 30em)">
                    <img srcset="img/ad003.png" alt="新手秘籍">
                </picture>
            </div>
        </div>
    </section>
</div>
</body>
<script src="node_modules/jquery/dist/jquery.js"></script>
<script src="node_modules/picturefill/dist/picturefill.min.js"></script>
<script src="node_modules/owl.carousel/dist/owl.carousel.min.js"></script>
<script>
  $(document).ready(function(){
    $('.owl-carousel').owlCarousel({
      items: 1,
      loop: true,
      autoplay: true,
      autoplayTimeout: 3000,
      autoplayHoverPause: true
    });
  });
</script>
</html>

附:如何挑选第三方插件:

1. 使用人数 2. 是否开源 3. 文档是否齐全 4. 活跃性 5. 小巧够用的组件(轻量级)

与君共勉:再牛逼的梦想,也抵不住傻逼般的坚持! 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个使用 Owl Carousel 实现大和小联动轮播的例子,你可以根据自己的需求进行修改和定制: HTML 代码: ```html <div class="owl-carousel owl-theme"> <div class="item"> <img src="big-image-1.jpg" alt="Big Image 1"> </div> <div class="item"> <img src="big-image-2.jpg" alt="Big Image 2"> </div> <div class="item"> <img src="big-image-3.jpg" alt="Big Image 3"> </div> </div> <div class="owl-carousel owl-theme thumbs"> <div class="item"> <img src="small-image-1.jpg" alt="Small Image 1"> </div> <div class="item"> <img src="small-image-2.jpg" alt="Small Image 2"> </div> <div class="item"> <img src="small-image-3.jpg" alt="Small Image 3"> </div> </div> ``` CSS 代码: ```css .owl-carousel { margin-bottom: 20px; } .thumbs .item { margin-right: 10px; } .thumbs .item:last-child { margin-right: 0; } .thumbs .item img { width: 100%; height: auto; cursor: pointer; opacity: 0.5; } .thumbs .item.active img { opacity: 1; } ``` JavaScript 代码: ```javascript $(document).ready(function() { var bigCarousel = $('.owl-carousel'); var thumbsCarousel = $('.thumbs'); bigCarousel.owlCarousel({ items: 1, loop: true, dots: false, nav: true, navText: ['<i class="fa fa-angle-left"></i>', '<i class="fa fa-angle-right"></i>'] }); thumbsCarousel.owlCarousel({ items: 3, loop: true, dots: false, nav: false, margin: 10, responsive: { 0: { items: 3 }, 600: { items: 4 }, 1000: { items: 5 } } }); thumbsCarousel.on('click', '.item', function(e) { e.preventDefault(); var index = $(this).index(); bigCarousel.trigger('to.owl.carousel', [index, 300, true]); thumbsCarousel.find('.item.active').removeClass('active'); $(this).addClass('active'); }); bigCarousel.on('changed.owl.carousel', function(event) { var index = event.item.index; thumbsCarousel.find('.item.active').removeClass('active'); thumbsCarousel.find('.item').eq(index).addClass('active'); }); }); ``` 在上面的例子中,我们使用了两个 Owl Carousel 实例,一个用于显示大,另一个用于显示小。我们通过 thumbsCarousel.on('click', '.item', function(e) {}); 和 bigCarousel.on('changed.owl.carousel', function(event) {}); 两个事件来实现大和小的联动效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值