javascript - 如何在 Angular2 中使用 jquery 和其他 .js 库

标签 javascript html css animation angular

我不知道这个问题是否属于Angular2。但我正在使用 Angular2 并想集成 this demo shown here - (please check its working demo and animated content)使用 HTML、CSS 和 JS。该演示展示了带有动画内部内容的 bootstrap 轮播。

This is my Demo in Plunker with Angular2 .我已尝试集成它并且一切正常,除了我看不到轮播内部内容的动画,如文章中所示。

另请指导如何在 Angular2 中使用 jQuery 和其他 JS 库?

index.html

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
    <link rel="stylesheet" href="/resources/demos/style.css">


    <script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.33.3/es6-shim.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.16/system-polyfills.js"></script>

    <script src="https://code.angularjs.org/2.0.0-beta.0/angular2-polyfills.js"></script>
    <script src="https://code.angularjs.org/tools/system.js"></script>
    <script src="https://code.angularjs.org/tools/typescript.js"></script>
    <script src="https://code.angularjs.org/2.0.0-beta.0/Rx.js"></script>
    <script src="https://code.angularjs.org/2.0.0-beta.0/angular2.dev.js"></script>



   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
    <!-- Demo scripts -->
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">

    <!-- Animate.css library -->
    <link href="css/animate.min.css" rel="stylesheet">


    <link href="css/demo.css" rel="stylesheet">
    <script src="js/demo.js"></script>

demo.js

/* Demo Scripts for Bootstrap Carousel and Animate.css article
* on SitePoint by Maria Antonietta Perna
*/
(function( $ ) {

    //Function to animate slider captions 
    function doAnimations( elems ) {
        //Cache the animationend event in a variable
        var animEndEv = 'webkitAnimationEnd animationend';

        elems.each(function () {
            var $this = $(this),
                $animationType = $this.data('animation');
            $this.addClass($animationType).one(animEndEv, function () {
                $this.removeClass($animationType);
            });
        });
    }

    //Variables on page load 
    var $myCarousel = $('#carousel-example-generic'),
        $firstAnimatingElems = $myCarousel.find('.item:first').find("[data-animation ^= 'animated']");

    //Initialize carousel 
    $myCarousel.carousel();

    //Animate captions in first slide on page load 
    doAnimations($firstAnimatingElems);

    //Pause carousel  
    $myCarousel.carousel('pause');


    //Other slides to be animated on carousel slide event 
    $myCarousel.on('slide.bs.carousel', function (e) {
        var $animatingElems = $(e.relatedTarget).find("[data-animation ^= 'animated']");
        doAnimations($animatingElems);
    });  

})(jQuery);

如何在 Angular2 应用程序中使用这个包含 jQuery 代码的 demo.js?

最佳答案

您必须在 angular2 组件初始化后运行您的 jQuery 代码。我对你的 app.component.ts 做了一点改动:

import {Component,OnInit} from 'angular2/core';
import {CORE_DIRECTIVES} from 'angular2/common';

declare var RunAnim;
declare var jQuery;

@Component({
    selector: 'my-app',
    templateUrl: 'mytemplate.html'
})
export class AppComponent implements OnInit { 

  constructor(){

  }
  ngOnInit(){
    RunAnim(jQuery);
  }

  ngAfterContentInit() {
  }
}

/*
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/

RunAnimjQuery 的声明是为了在 TS 中可见它们是全局可用的。

我为你的 jQuery 代码创建了一个命名函数 RunAnim:

/* Demo Scripts for Bootstrap Carousel and Animate.css article
* on SitePoint by Maria Antonietta Perna
*/
(window.RunAnim=function( $ ) {

    //Function to animate slider captions 
    function doAnimations( elems ) {
        //Cache the animationend event in a variable
        var animEndEv = 'webkitAnimationEnd animationend';

        elems.each(function () {
            var $this = $(this),
                $animationType = $this.data('animation');
            $this.addClass($animationType).one(animEndEv, function () {
                $this.removeClass($animationType);
            });
        });
    }

    //Variables on page load 
    var $myCarousel = $('#carousel-example-generic'),
        $firstAnimatingElems = $myCarousel.find('.item:first').find("[data-animation ^= 'animated']");

    //Initialize carousel 
    $myCarousel.carousel();

    //Animate captions in first slide on page load 
    doAnimations($firstAnimatingElems);

    //Pause carousel  
    $myCarousel.carousel('pause');


    //Other slides to be animated on carousel slide event 
    $myCarousel.on('slide.bs.carousel', function (e) {
        var $animatingElems = $(e.relatedTarget).find("[data-animation ^= 'animated']");
        doAnimations($animatingElems);
    });  

});

关于javascript - 如何在 Angular2 中使用 jquery 和其他 .js 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35769528/

相关文章:

html - 父引导行中的中心 div

html - 根据 Angular 字大小自动调整行的大小

javascript - 如何在 Kendo UI 网格中添加下拉列表

php - 制作易于编辑的最佳方法 "thermometer for donations"

php - 基于数据库数组PHP自动检查复选框

html - 无论窗口大小如何,无论侧边栏是否存在,内容 div 始终居中对齐

javascript - jQuery在点击时改变多个元素的颜色

javascript - 使用 jquery addClass、添加文本和删除 li 的对象格式

javascript - S3 预签名 URL 问题 - 文件上传成功,200 statusCode 但没有响应正文

html - 使用 flexbox 在跨度和段落之间创建一条水平线