【问题标题】:JQuery not working on Bootstrap Carousel not displayed imagesJQuery 不能在 Bootstrap Carousel 上工作,不显示图像
【发布时间】:2015-11-24 11:18:03
【问题描述】:

我在这个问题上苦苦挣扎了好几个小时。我正在使用 Bootstrap3 轮播;我想做的是在我的 .toBottom div。

1)这是轮播的基本HTML结构:

<div id="myCarousel" class="carousel slide" data-ride="carousel">

  <div class="carousel-inner" role="listbox">
    <div class="item active">
      <img />
    </div>

    <div class="item">
      <img />
    </div>

    <div class="item toBottom">
      <img />
    </div>

    <div class="item">
      <img />
    </div>
  </div>

</div>


2) JQuery 脚本基本上为 .toBottom 元素计算新的 CSS“顶部”值。


3)我注意到,默认情况下,也有 .active 类的 .item div 有

display: block;

所有剩余的“非活动” .item div 都有

display: none;


事实是,如果我在 .toBottom 为 .active 时启动 JQuery 脚本,CSS 顶部值会正常更改,BUT 如果我在它不是 .active 时启动它(或者它是 display:none),它的 CSS 根本不会改变
我该如何解决这个问题?即使 .toBottom div 是 display:none,我也想应用该规则。
提前谢谢您。

(如果我不太清楚,我很抱歉,我试图尽可能简化情况)


编辑:

这是脚本:

var carouselHeight = $(".carousel-inner").outerHeight();
var newTop = carouselHeight - $(".item.toBottom > img").height();
if(newTop < 0)
  $(".item.toBottom > img").css("top", newTop);
else
  $(".item.toBottom > img").css("top", "");

请注意,如果$(".item.toBottom").css("display") == "block",此代码有效



EDIT2:

终于解决了。问题是我无法计算 $(".item.toBottom &gt; img").height(),因为 .item.toBottom 没有显示。这是最终的脚本:

  var carouselHeight = $(".carousel-inner").outerHeight();
  $(".item.toBottom").css('display','block');
  var newTop = carouselHeight - $(".item.toBottom > img").height();
  $(".item.toBottom").css('display','');
  if(newTop < 0)
    $(".item.toBottom > img").css("top", newTop);
  else
    $(".item.toBottom > img").css("top", '');

特别感谢 @Zorken17 的想法。

【问题讨论】:

  • 向我们展示你的javascript
  • @2pha 我将其添加到原帖中
  • .toBottom 里面是什么?也许也显示该代码
  • 使用 $(".item.toBottom").show()$(".item.toBottom").hide() 代替 css 属性。

标签: jquery twitter-bootstrap-3 bootstrap-carousel


【解决方案1】:

如果没有显示somthin,则无法计算高度,因为高度将为0。

您可以尝试等待.activ类,然后计算。

if( $('.item.toBottom').hasClass('active')) {
    var carouselHeight = $(".carousel-inner").outerHeight();
    var newTop = carouselHeight - $(".item.toBottom > img").height();
    if(newTop < 0) {
        $(".item.toBottom > img").css("top", newTop);
    } else {
        $(".item.toBottom > img").css("top", "");
    }
}

【讨论】:

  • 非常感谢。我接近的解决方案略有不同,但这个想法是你的。我将它添加到原始帖子中。
猜你喜欢
  • 2017-07-12
  • 1970-01-01
  • 2012-03-04
  • 1970-01-01
  • 2014-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多