如何将css3动画添加到bootstrap中

author author     2023-02-23     668

关键词:

很多时候,如果你的项目需要的是一个轻量级的轮播,不需要很多的功能。同时你的项目是采用Bootstrap,(一个最流行的开源前端框架)的话。你可以参考一下bootstrap官方组件。
介绍Animate.css
为了让我自己写的动画效果值得称赞,我用一个非常有名的开源的CSS3动画库,被形象的称为animate.css。 Dan Eden写的。
这是让我能专注于手头的任务,而不是解释CSS3动画的代码。
用Animate.css 需要2个步骤:
在html文档中引入animate.min.css。
在网页中要加动画的元素上添加animated yourchosenanimation类。
接下来你用Animate.css网站上的看到的关于动画的类名,代替yourchosenanimation。
引入Bootstrap轮播组件
Bootstrap轮播组件有三个主要的部分。
轮播指示显示幻灯的页面数量,给用户提供一个视觉线索,并提供可以滑动的导航。
轮播条目,一个叫.carousel-inner的类,包含在外边框的里边。代表每一个独立的滑块。每个图片里边的都可以放置图片。也可以添加标题。还可以在html元素上添加carousel-caption类名。Bootstrap会有自带的样式。我们可以通过这些元素添加动画。
最后,是轮播控制箭头,功能是可以使用户前后滑动。

如果想了解更多Bootstrap轮播组件的详情,可以查看Syed Fazle Rahman的用Bootstrap3创建js轮播效果这篇文章。
为了简单的展示demo,就先不加图片了。焦点先放在轮播框架上作为动画。
构建HTML结构
下边是你需要引用到你项目当中的:
jQuery
Bootstrap's CSS and JavaScript
Animate.css
一个样式表和js文档。

为了加快开发进程,从Bootstrap官网引用了模板和必要的文件。
下边是Bootstrap轮播代码:<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active">
</li>
<li data-target="#carousel-example-generic" data-slide-to="1"></li>
<li data-target="#carousel-example-generic" data-slide-to="2"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">

<!-- First slide -->
<div class="item active">
<div class="carousel-caption">
<h3 data-animation="animated bounceInLeft">
This is the caption for slide 1
</h3>
<h3 data-animation="animated bounceInRight">
This is the caption for slide 1
</h3>
<button class="btn btn-primary btn-lg"
data-animation="animated zoomInUp">Button</button>
</div>
</div><!-- /.item -->

<!-- Second slide -->
<div class="item">
<div class="carousel-caption">
<h3 class="icon-container" data-animation="animated bounceInDown">
<span class="glyphicon glyphicon-heart"></span>
</h3>
<h3 data-animation="animated bounceInUp">
This is the caption for slide 2
</h3>
<button class="btn btn-primary btn-lg"
data-animation="animated zoomInRight">Button</button>
</div>
</div><!-- /.item -->

<!-- Third slide -->
<div class="item">
<div class="carousel-caption">
<h3 class="icon-container" data-animation="animated zoomInLeft">
<span class="glyphicon glyphicon-glass"></span>
</h3>
<h3 data-animation="animated flipInX">
This is the caption for slide 3
</h3>
<button class="btn btn-primary btn-lg"
data-animation="animated lightSpeedIn">Button</button>
</div>
</div><!-- /.item -->
</div><!-- /.carousel-inner -->

<!-- Controls -->
<a class="left carousel-control" href="#carousel-example-generic"
role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>

<a class="right carousel-control" href="#carousel-example-generic"
role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>

</div><!-- /.carousel -->

如果以上代码没有错,你在浏览器打开会看到一个可以运行的轮播,上边的一切不包含一行javascript代码。如果你不添加任何图像,只是在css文档给.carousel .item这个类块添加min-height值防止轮播塌陷。
在轮播标题内的元素添加一个动画属性data-animation,用这个特别的动画类库作为他们的值。
如果你想从Animate.css库体验其他的动画,用你选择的动画类名代替data-animation属性值。
我们在javascript代码中用data-animation属性值。
虽然一个简单的自动轮播在一些案例中可以找到,但是对于这个案例我们有更多的控制。
在这个方向的第一步,从元素中删除data-ride="carousel"值,把data-ride属性值初始化儿不用写任何代码。但是,我们打算用js代码控制轮播,因此,这个data-ride属性就不必要了。
给轮播加CSS样式
现在根据自己的喜好,发挥创造力给轮播标题添加样式。我将要写的样式规则是能顺畅工作的demo。
更具体的说,我们增加动画延迟属性的控制。定义每个动画什么时候开始(注意为了简单演示,省略了浏览器前缀)
.carousel-caption h3:first-child
animation-delay: 1s;


.carousel-caption h3:nth-child(2)
animation-delay: 2s;


.carousel-caption button
animation-delay: 3s;


上面的代码片段中确保元素动画有序开始,还可以做其他的效果。例如,你可以选择前两个标题同时出现,然后是button按钮,可以自己决定,享受乐趣吧。
写jQuery代码:
开始初始化这个轮播,在你的自定义的javascript 文件中添加一下代码:
var $myCarousel = $('#carousel-example-generic');
// Initialize carousel
$myCarousel.carousel();

我们已经动态的设置了轮播,接下来,我们来解决这个动画。
为了使第一个幻灯片的标题有动画,当页面在浏览器加载完后脚本得运行。随后的幻灯片在动画下进入到我们的视野,我们的代码在slide.bs.carousel 事件上运行。意味着同样的代码运行两次:页面加载一次和slide.bs.carousel 事件一次。
因为我们喜欢遵循不重复的原则,我们打算把我们的代码封装在函数中,并在适当的时候引用。
代码:
function doAnimations(elems)
var animEndEv = 'webkitAnimationEnd animationend';
elems.each(function ()
var $this = $(this),
$animationType = $this.data('animation');
// Add animate.css classes to
// the elements to be animated
// Remove animate.css classes
// once the animation event has ended
$this.addClass($animationType).one(animEndEv, function ()
$this.removeClass($animationType);
);
);

// Select the elements to be animated
// in the first slide on page load
var $firstAnimatingElems = $myCarousel.find('.item:first')
.find('[data-animation ^= "animated"]');
// Apply the animation using our function
doAnimations($firstAnimatingElems);
// Pause the carousel
$myCarousel.carousel('pause');

// Attach our doAnimations() function to the
// carousel's slide.bs.carousel event
$myCarousel.on('slide.bs.carousel', function (e)
// Select the elements to be animated inside the active slide
var $animatingElems = $(e.relatedTarget)
.find("[data-animation ^= 'animated']");
doAnimations($animatingElems);
);

上边的代码 我们来分析一下。
1、来看doAnimations()函数
这个doAnimations() 函数执行的任务如下。
它开始通过缓存变量中含有的animationend事件名称的字符串。这个事件告诉我们,你可能已经猜到,当每个动画结束。我们需要这个点的信息,因为每一次的动画结束后,我们将animate.css类移除。如果我们不做移除,轮播的标题将只有一次动画,也就是,只是在第一次轮播显示特定的幻灯片。
var animEndEv = 'webkitAnimationEnd animationend';

接来下,我们的函数循环遍历每一个我们想要有动画的元素,并获取data-animation的属性值。想上边所说的,这个值包含我们想要添加给元素的Animate.css类,以便有动画效果。
elems.each(function ()
var $this = $(this),
$animationType = $this.data('animation');
// etc...
);

最后,这个doAnimations() 函数动态添加animate.css类的每个要执行动画的元素上,当动画结束的时候,还附加了一个事件监听。动画结束后我们移除从Animate.css添加的类。这样确保下一个轮播灯片回到当前的区域。(你试着删除这段代码,看看会发生什么)
$this.addClass($animationType).one(animEndEv, function ()
$this.removeClass($animationType);
);

2、第一个标题的动画
当页面在浏览器中加载时,我们在第一个幻灯片中动画的内容:
var $firstAnimatingElems = $myCarousel.find('.item:first')
.find("[data-animation ^= 'animated']");
doAnimations($firstAnimatingElems);

在这个代码中,我们找到第一张灯片,我们希望通过使用data-animation从动画的标题获取动画属性的值。然后我们把变量 $firstAnimatingElems 当做参数传给doAnimations()函数,然后执行函数。
3、轮播的停止功能
当第一张灯片内容执行完动画以后,我们停止这个轮播功能。
$myCarousel.carousel('pause');

这是Bootstrap轮播组件防止不停旋转的特征。不停的旋转,可能会让访客生厌。
在这种情况下,我建议确保轮播不直接循环到下一个灯片直到所有的动画运行完毕。可以通过设置在初始化代码中的“间隔”选项来控制这个:
$myCarousel.carousel(
interval: 4000
);

在我看来,一个无限循环轮播标题跳跃每一次的滑动进入视线不理想。
4、轮播幻灯片标题的动画
为每张幻灯片的动画轮播标题变得可见需要以下描述的步骤。
首先,我们在slide.bs.carousel上添加一个事件监听器。
当幻灯片实例方法被调用时,该事件立即触发。
$myCarousel.on('slide.bs.carousel', function (e)
// do stuff...
);

接下来,我们选择当前的灯片,找到我们希望增加动画的元素。下边的代码用了slide.bs.carousel事件的.relatedTarget属性来绑定动画。
var $animatingElems = $(e.relatedTarget).find("[data-animation ^= 'animated']");

最后,我们调用doAnimations()函数,把$animatingElems当做参数传进去。
doAnimations($animatingElems);

正如你们许多人可能知道,轮播有一些需要开发者考虑的问题。
参考技术A bootstart是一个封装框架,封装的本就是一些CSS特效和JS代码,你要改写添加这个框架,非作者本人怎么做到呢,除非这个框架留有接口,再说你何必执着添加呢,正常使用bootstart,达不到的效果再自己加入css3的不就完了,bootstart我不熟悉,不过认为跟jq是一个类型的吧, 参考技术B <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active">
</li>
<li data-target="#carousel-example-generic" data-slide-to="1"></li>
<li data-target="#carousel-example-generic" data-slide-to="2"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">

<!-- First slide -->
<div class="item active">
<div class="carousel-caption">
<h3 data-animation="animated bounceInLeft">
This is the caption for slide 1
</h3>
<h3 data-animation="animated bounceInRight">
This is the caption for slide 1
</h3>
<button class="btn btn-primary btn-lg"
data-animation="animated zoomInUp">Button</button>
</div>
</div><!-- /.item -->

<!-- Second slide -->
<div class="item">
<div class="carousel-caption">
<h3 class="icon-container" data-animation="animated bounceInDown">
<span class="glyphicon glyphicon-heart"></span>
</h3>
<h3 data-animation="animated bounceInUp">
This is the caption for slide 2
</h3>
<button class="btn btn-primary btn-lg"
data-animation="animated zoomInRight">Button</button>
</div>
</div><!-- /.item -->

<!-- Third slide -->
<div class="item">
<div class="carousel-caption">
<h3 class="icon-container" data-animation="animated zoomInLeft">
<span class="glyphicon glyphicon-glass"></span>
</h3>
<h3 data-animation="animated flipInX">
This is the caption for slide 3
</h3>
<button class="btn btn-primary btn-lg"
data-animation="animated lightSpeedIn">Button</button>
</div>
</div><!-- /.item -->
</div><!-- /.carousel-inner -->

<!-- Controls -->
<a class="left carousel-control" href="#carousel-example-generic"
role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>

<a class="right carousel-control" href="#carousel-example-generic"
role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>

</div><!-- /.carousel -->

如何动态地将动画添加到队列中

】如何动态地将动画添加到队列中【英文标题】:Howtoaddanimationtoaqueuedynamically【发布时间】:2018-04-1717:09:46【问题描述】:我有以下情况:我有一个圆形uiview对象,例如,我每1秒得到一个新点。我对从uiview的当前位置到新点的... 查看详情

css将css3动画添加到您的genesis主题(代码片段)

查看详情

css将css3动画添加到您的genesis主题(代码片段)

查看详情

css将css3动画添加到您的genesis主题(代码片段)

查看详情

如何将动画 css 添加到我的 wordpress 主题中[重复]

】如何将动画css添加到我的wordpress主题中[重复]【英文标题】:Howtoaddanimatecsstomywordpresstheme[duplicate]【发布时间】:2019-11-2102:43:53【问题描述】:我已经开始制作wordpress主题,并且想为我的块使用一些动画,“动画css库”我试图... 查看详情

Bootstrap - 如何将徽标添加到导航栏类?

】Bootstrap-如何将徽标添加到导航栏类?【英文标题】:Bootstrap-Howtoaddalogotonavbarclass?【发布时间】:2015-04-0603:57:51【问题描述】:我想在导航栏品牌的顶部导航栏中添加一个徽标。我希望它与视口大小一起缩放,所以我使用img-res... 查看详情

如何将另一个矩阵雨代码动画添加到我的画布动画中?

】如何将另一个矩阵雨代码动画添加到我的画布动画中?【英文标题】:HowdoIaddanotherMatrixraincodeanimationtomycanvasanimation?【发布时间】:2012-03-0413:23:57【问题描述】:我正在尝试使用画布元素和javascript来模拟矩阵代码雨。我可以一... 查看详情

如何在 JS 项目中使用 Wrap Bootstrap?我有一个引导主题,但不清楚如何将其添加到项目中

】如何在JS项目中使用WrapBootstrap?我有一个引导主题,但不清楚如何将其添加到项目中【英文标题】:HowdoIuseWrapBootstrapinaJSproject?Ihaveabootstrapthemebutitisn\'tclearhowtoaddittotheproject【发布时间】:2017-05-1201:51:26【问题描述】:我有兴趣... 查看详情

使用 bootstrap-vue:如何将类添加到 b 分页组件中的页面按钮

】使用bootstrap-vue:如何将类添加到b分页组件中的页面按钮【英文标题】:Withbootstrap-vue:Howtoaddaclasstothepagebuttonsinab-paginationcomponent【发布时间】:2019-06-1209:20:22【问题描述】:假设我有一个.vue文件,我在我的vue组件中使用bootstrap... 查看详情

如何将自定义动画添加到 ContextMenuStrip?

】如何将自定义动画添加到ContextMenuStrip?【英文标题】:HowtoaddcustomanimationstoaContextMenuStrip?【发布时间】:2021-10-1222:22:20【问题描述】:有没有办法在打开ContextMenuStrip时创建动画,就像在这张图片中一样?这个动画是WhatsApp右键... 查看详情

将 bootstrap.js 添加到 browserify?

】将bootstrap.js添加到browserify?【英文标题】:Addingbootstrap.jstobrowserify?【发布时间】:2014-05-2219:09:15【问题描述】:所以我想弄清楚如何做到这一点?我通过bower下载了bootstrap-sass并将bootstrapjavascript添加到shim中。有几件事让我很... 查看详情

将一个类动态添加到 Bootstrap 的“popover”容器中

】将一个类动态添加到Bootstrap的“popover”容器中【英文标题】:DynamicallyaddaclasstoBootstrap\'s\'popover\'container【发布时间】:2012-08-2314:47:09【问题描述】:我已经彻底搜索过***和Google,但结果都是空的。因此,如果已经提出并解决... 查看详情

如何将自定义页脚添加到 Bootstrap-Vue 表

】如何将自定义页脚添加到Bootstrap-Vue表【英文标题】:HowtoaddaCustomFootertoaBootstrap-Vuetable【发布时间】:2019-08-1805:48:03【问题描述】:我有一个表格,其中包含我想要添加页脚的数据,以便在每行中相加并显示一个int值的总计。... 查看详情

如何将变量添加到卡中的图像链接(vue-bootstrap)?

】如何将变量添加到卡中的图像链接(vue-bootstrap)?【英文标题】:HowcanIaddavariabletoanimagelinkinacard(vue-bootsrap)?【发布时间】:2022-01-1920:47:00【问题描述】:我正在尝试将取自API的id添加到图像url的末尾,以便循环中的每张卡片都... 查看详情

如何将 Bootstrap 4 添加到库 django-summernote?

】如何将Bootstrap4添加到库django-summernote?【英文标题】:HowcanIaddBootstrap4tothelibrarydjango-summernote?【发布时间】:2018-09-2119:33:27【问题描述】:我需要将Bootstrap4添加到django-summernote。这可能吗?【问题讨论】:【参考方案1】:Django... 查看详情

如何将事件侦听器添加到动画变换旋转?

】如何将事件侦听器添加到动画变换旋转?【英文标题】:Howtoaddeventlistenertoanimatedtransformrotate?【发布时间】:2019-02-0815:30:07【问题描述】:所以我有一张2面卡片,每次卡片旋转180度时,它都会改变面的值。我想做的一件事是在... 查看详情

将显示与 css3 动画一起使用?如何在动画期间/之后隐藏/取消隐藏元素?

】将显示与css3动画一起使用?如何在动画期间/之后隐藏/取消隐藏元素?【英文标题】:Usingdisplaywithcss3animations?Howtohide/unhideelementsduring/afteranimations?【发布时间】:2014-08-2110:05:40【问题描述】:我有一个div,我需要将它的不透明... 查看详情

Vue将项目添加到带有动画的列表中

...的Vue应用程序。现在我想要一些漂亮的动画,但我不知道如何使用Vue来做到这一点。您可以在下面找到代表这个Vue应用程序的代码。每2秒推送一些数据,这些数据将通过Vue列表渲染添加到HTML。在此示例中,当前D 查看详情