【问题标题】:Owl Carousel 2 - Center and overlayOwl Carousel 2 - 中心和覆盖
【发布时间】:2015-03-09 19:17:20
【问题描述】:

我想要一个三项目轮播,中间项目中心为一种背景颜色(稍后在背景图像中显示颜色),左+右项目为不同颜色(稍后在带有灰度过滤器的图像背景上)。像这个是 http://codepen.io/OwlFonk/pen/dpjhB 的 owl 1 的,但我想使用 Owl 2。

我似乎无法挑出当前项目?!我的旋转木马一直在“画”中间和右边……我做错了什么?!越来越沮丧!! =)

<!DOCTYPE html>
<html lang="en">
  <head>
    <!-- head -->
    <meta charset="utf-8">
    <meta name="msapplication-tap-highlight" content="no" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <title>Owl 2 - Color Overlay</title>

    <!-- Stylesheets -->
    <link href="http://www.owlcarousel.owlgraphic.com/assets/owlcarousel/assets/owl.carousel.min.css" rel="stylesheet">
    <link href="http://www.owlcarousel.owlgraphic.com/assets/owlcarousel/assets/owl.theme.default.min.css" rel="stylesheet">
    <!--<link href="owl.carousel.min.css" rel="stylesheet">-->
    <!--<link href="owl.theme.default.min.css" rel="stylesheet">-->

    <!-- javascript -->
    <script src="http://www.owlcarousel.owlgraphic.com/assets/vendors/jquery.min.js"></script>
    <script src="http://www.owlcarousel.owlgraphic.com/assets/owlcarousel/owl.carousel.js"></script>
    <!--<script src="jquery-2.1.1.min.js"></script>-->
    <!--<script src="owl.carousel-2.0.0.js"></script>-->

<style>
.content{
    position:absolute; 
    background-color: rgba(255, 255, 255, 0.7);
    filter: alpha(opacity=70);
    padding:20px;
    width:auto;
    left:5%;
    bottom:0%;
    }
.header{
    font-family:Arial;
    color:#c70c6f; 
    font-size:2em;
    }
.text{
    font-family:Arial;
    color:#000; 
    font-size:1em;
    }
<!--preloading images-->
.owl-item.loading {
    min-height: 150px;
    background: url(AjaxLoader.gif) no-repeat center center;
}   
#owl-demo .owl-item > div img {
  margin: 0;
  height:100px;
  background: #cccccc;
}
#owl-demo .owl-item > div {
  margin: 0;
  height:100px;
  background: #66cc22;
}
#owl-demo .owl-item.active > div {
  margin: 0;
  height:100px;
  background: #668899;
}
</style>

  </head>
  <body style="padding: 0; margin: 0;">

    <!-- body -->
        <div id="demo" >

          <div id="owl-demo" class="owl-carousel" style="width:100%">

            <div  style="position:relative; width:960px;height:340px;margin-left:0px;margin-right:0px;">
                <div class="content" >
                    <span class="header">Header 1</span><br>
                    <span class="text">Lorem ipsum and so on...</span>
                </div>
            </div>
            <div style="position:relative; width:960px;height:340px;margin-left:0px;margin-right:0px;">
                    <div class="content" >
                    <span class="header">Header 2</span><br>
                    <span class="text">Lorem ipsum and so on...</span>
                </div>  
            </div>
            <div style="position:relative; width:960px;height:340px;margin-left:0px;margin-right:0px;">
                <div class="content" >
                    <span class="header">Header 3</span><br>
                    <span class="text">Lorem ipsum and so on...</span>
                </div>
            </div>
            <div style="position:relative; width:960px;height:340px;margin-left:0px;margin-right:0px;">
                <div class="content" >
                    <span class="header">Header 4</span><br>
                    <span class="text">Lorem ipsum and so on...</span>
                </div>
            </div>
            <div style="position:relative; width:960px;height:340px;margin-left:0px;margin-right:0px;">
                <div class="content" >
                    <span class="header">Header 5</span><br>
                    <span class="text">Lorem ipsum and so on...</span>
                </div>
            </div>
            <div style="position:relative; width:960px;height:340px;margin-left:0px;margin-right:0px;">
                <div class="content" >
                    <span class="header">Header 6</span><br>
                    <span class="text">Lorem ipsum and so on...</span>
                </div>
            </div>
          </div>
        </div>

    <script>
        var owl = $('.owl-carousel');   
        owl.owlCarousel({

        width:960,
        margin:5,
        dots:false,
        loop: true,
        autoplay:true,
        autoplayTimeout:3000,
        autoplayHoverPause:true,
        autoWidth:true,

        afterAction: function(el){  
            //remove class active
            this
            .$owlItems  
            .removeClass('active')

            //add class active
            this
            .$owlItems 
            .eq(this.currentItem + 1)
            .addClass('active')
        },

        //responsive
        responsiveClass:true,
        responsive:{
            0:{
                items:1,
                center:false,
                singleItem:true,
                itemsScaleUp:true,
            },
            960:{
                items:3,
                center:true,
                singleItem:false,
                itemsScaleUp:false,
            }
        }
    })
    </script>
  </body>

</html>

【问题讨论】:

  • Owl 2 将active 类应用于所有可见元素。您正在与之抗争,并且可能应该使用不同的类值(名称)。

标签: jquery css owl-carousel


【解决方案1】:
<!DOCTYPE html>
<html lang="en">
<head>
<!-- head -->
<meta charset="utf-8">
<meta name="msapplication-tap-highlight" content="no" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />

<title>Owl2</title>

<!-- Stylesheets -->
<link href="http://www.owlcarousel.owlgraphic.com/assets/owlcarousel/assets/owl.carousel.min.css" rel="stylesheet">
<link href="http://www.owlcarousel.owlgraphic.com/assets/owlcarousel/assets/owl.theme.default.min.css" rel="stylesheet">

<!-- javascript -->
<script src="http://www.owlcarousel.owlgraphic.com/assets/vendors/jquery.min.js"></script>
<script src="http://www.owlcarousel.owlgraphic.com/assets/owlcarousel/owl.carousel.js"></script>


<style>
.content{
    position:absolute; 
    background-color: rgba(255, 255, 255, 0.7);
    filter: alpha(opacity=70);
    padding:20px;
    width:auto;
    left:5%;
    bottom:0%;
    }
.header{
    font-family:Arial;
    color:#c70c6f; 
    font-size:2em;
    }
.text{
    font-family:Arial;
    color:#000; 
    font-size:1em;
    }

.owl-item > div img {
  margin: 0;
  height:100px;
  background: #cccc66;
}
.owl-item.center > div {
  margin: 0;
  height:100px;
  background: #11cccc;
}
.owl-item:not(.center) > div {
  margin: 0;
  height:100px;
  background: #cc33cc;
}

</style>

  </head>
  <body style="padding: 0; margin: 0;">

    <!-- body -->
        <div id="demo">

          <div id="owl-demo" class="owl-carousel" style="width:100%">

            <div class="item" style="position:relative; width:960px;height:340px;margin-left:0px;margin-right:0px;" />
                <div class="content" />
                    <span class="header">Header 1</span><br>
                    <span class="text">Lorem ipsum and so on...</span>
                </div>
            </div>

            <div class="item"  style="position:relative; width:960px;height:340px;margin-left:0px;margin-right:0px;">
                <div class="content" >
                    <span class="header">Header 2</span><br>
                    <span class="text">Lorem ipsum and so on...</span>
                </div>  
            </div>

            <div class="item"  style="position:relative;width:960px;height:340px;margin-left:0px;margin-right:0px;">
                <div class="content" >
                    <span class="header">Header 3</span><br>
                    <span class="text">Lorem ipsum and so on...</span>
                </div>
            </div>
            <div style="position:relative; width:960px;height:340px;margin-left:0px;margin-right:0px;">
                <div class="content" >
                    <span class="header">Header 4</span><br>
                    <span class="text">Lorem ipsum and so on...</span>
                </div>
            </div>
            <div style="position:relative; width:960px;height:340px;margin-left:0px;margin-right:0px;">
                <div class="content" >
                    <span class="header">Header 5</span><br>
                    <span class="text">Lorem ipsum and so on...</span>
                </div>
            </div>
            <div style="position:relative; width:960px;height:340px;margin-left:0px;margin-right:0px;">
                <div class="content" >
                    <span class="header">Header 6</span><br>
                    <span class="text">Lorem ipsum and so on...</span>
                </div>
            </div>
          </div>
        </div>

    <script>    
     jQuery(document).ready(function($) {

        var owl = $('.owl-carousel');
        owl.owlCarousel({
            dots:false,
            width:960,
            margin:1,       
            loop: true,
            autoplay:true,
            autoplayTimeout:3000,
            autoplayHoverPause:true,
            autoWidth:true,
            center:true,

            //responsive
            responsiveClass:true,
            responsive:{
                0:{
                    items:1,
                    center:false,
                    singleItem:true,
                    itemsScaleUp:true,
                },
                960:{
                    items:3,
                    center:true,
                    singleItem:false,
                    itemsScaleUp:false,
                }
            }
        });

    });
    </script>

  </body>
</html>

【讨论】:

  • 工作! .active 类针对每个 owl-item。类中心仅针对中心项。
【解决方案2】:

由于 Owl Carousel 2 将活动类应用于所有可见元素,我倾向于使用 jQuery 的 eq() 方法按索引定位第二个元素:

afterMove: function(el) { 
    $('owl-item').removeClass('my-other-color-class');
    $('.owl-item.active').eq(1).addClass('my-other-color-class');
},

【讨论】:

  • 谢谢@isherwood ...虽然没有做到这一点。 =( 似乎第三项(右)在用中心颜色“绘制”之前以正确的颜色闪烁......我的“计划 b”是在移动后用正确的颜色覆盖它,但我无法到达它...
  • 如果您想创建一个我们可以进行的演示,我很乐意提供进一步的帮助。 jsfiddle.net
  • 我会说谢谢,但是您重复的内联样式使我无法轻松地将滑块面板制作得足够小以在演示中看到。请使用 CSS 类进行纠正。
  • 这不是一件小事。将所有内联样式移动到 CSS 面板(因为内联样式很麻烦)并更改大小以使其更适合。理想情况下,您的小提琴适合演示区域,无需水平滚动。
  • 我设置了这个,然后我意识到猫头鹰 2 不能通过 CDN 获得。无论如何,这是一个可行的演示的开始。我今天没时间了。 jsfiddle.net/isherwood/xb0tnwo3/7
猜你喜欢
  • 1970-01-01
  • 2020-01-11
  • 2018-06-07
  • 1970-01-01
  • 1970-01-01
  • 2015-04-01
  • 1970-01-01
  • 2016-02-14
  • 2020-05-31
相关资源
最近更新 更多