angular7中使用swiper轮播图插件

swiper中文官网

https://www.swiper.com.cn/

安装 插件

npm / cpm

npm install swiper --save  || yarn add swiper --save
npm install @types/swiper --save || yarn add @types/swiper --save
配置
  1. angular.json

    "styles": [
       "src/styles.scss",
       "src/assets/iconfont/iconfont.css",
       "node_modules/swiper/dist/css/swiper.min.css" // 引入css文件
     ],
     "scripts": [
         "node_modules/echarts/dist/echarts.js",
         "node_modules/swiper/dist/js/swiper.min.js" // 引入js文件
     ],
    
使用

ts文件中

import Swiper from 'swiper';

export class HomeComponent implements OnInit {

    // 轮播数据
    swiperList: any[] = [];

    constructor() { }

    ngOnInit() {

        // 轮播图初始化
        setTimeout(() => {
            this.swiperInit();
        }, 0);

        this.swiperList = [
            {
                imgUrl: 'http://www.yyth.com.cn/uploadDir/jpg/20190111/1547185316391.jpg'
            },
            {
                imgUrl: 'http://www.yyth.com.cn/uploadDir/jpg/20181229/1546050204895.jpg'
            },
            {
                imgUrl: 'http://www.yyth.com.cn/uploadDir/jpg/20181229/1546061999592.jpg'
            }
        ];
    }

    // 轮播图初始化
    swiperInit() {
        // tslint:disable-next-line: no-unused-expression
        new Swiper ('.swiper-container', {
            direction: 'horizontal', // 水平切换选项
            loop: true, // 循环模式选项
            speed: 600, // 滑动时间
            grabCursor: true, // 鼠标指针形状
            autoplay: {
                delay: 1500,
                stopOnLastSlide: false,
                disableOnInteraction: false // 手动滑动之后依然自动轮播
            }, // 自动滑动
            effect : 'fade', // 切换效果
            observer: true, // 修改swiper自己或子元素时,自动初始化swiper
            observeParents: true, // 修改swiper的父元素时,自动初始化swiper
            // navigation: {  // 分页器
            //     nextEl: '.swiper-button-next',
            //     prevEl: '.swiper-button-prev',
            // },
            // pagination: { // 如果需要前进后退按钮
            //     el: '.swiper-pagination',
            // },
            // scrollbar: { // 如果需要滚动条
            //     el: '.swiper-scrollbar',
            // }
        });
    }

}

html

<div class="swiper-container">
    <divclass="swiper-wrapper">
        <ng-container *ngFor="let item of swiperList">
            <div class="swiper-slide">
            	 <!-- <img [src]="item.imgUrl" alt=""> -->
                <a href="javascript:;" [ngStyle]="{'background': 'url(' + item.imgUrl + ')'}"></a>
            </div>
        </ng-container>
    </div>
    <!-- 如果需要分页器 -->
    <!-- <div class="swiper-pagination"></div> -->
    
    <!-- 如果需要导航按钮 -->
    <!-- <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div> -->
    
    <!-- 如果需要滚动条 -->
    <!-- <div class="swiper-scrollbar"></div> -->
</div>
常见问题

上述是通过以下这种方式来引入swiper的,但是在IE11浏览器中会报错:

import Swiper from 'swiper';

解决方法:

在typings.d.ts文件内 声明全局的Swiper对象。全局的对象一般都放在这里声明,声明后会自动关联到Swiper.js文件的Swiper对象

declare var Swiper: any;
借鉴地址

https://blog.csdn.net/wgp15732622312/article/details/77435703?foxhandler=RssReadRenderProcessHandler

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值