Html5+Ant-Motion实现官网Banner视频、图片轮播

2 篇文章 0 订阅

轮播banner使用的动画库:Ant-Motion

官方给了很多小demo,可以随意捣鼓
在这里插入图片描述
实现后的效果如下图(gif导出比较大,所以看上去卡卡的,实际很流畅)
在这里插入图片描述

播放视频采用的是Html5中的video
 <video
       style={{ width: "100%", height: "100%", objectFit: "cover" }}
       autoPlay
       muted
       loop
>
       <source src={item.video} type="video/mp4"></source>
</video>
  1. autoplay 如果出现该属性,则视频在就绪后马上播放。
  2. controls 如果出现该属性,则向用户显示控件,比如播放按钮。
  3. height 设置视频播放器的高度。
  4. loop 如果出现该属性,则当媒介文件完成播放后再次开始播放。
  5. muted 规定视频的音频输出应该被静音。
  6. poster 规定视频下载时显示的图像,或者在用户点击播放按钮前显示的图像。
  7. preload 如果出现该属性,则视频在页面加载时进行加载,并预备播放。如果使用 “autoplay”,则忽略该属性。
  8. src 要播放的视频的 URL。
  9. width 设置视频播放器的宽度。
Banner轮播

1、封装banner组件

import React from "react";
import { useRoute } from "@/hooks";
import { Button } from "antd";
import { RightOutlined } from "@ant-design/icons";

import BannerAnim, { Element } from "rc-banner-anim";
import TweenOne from "rc-tween-one";
import FontText from "../html/FontText";

import "./index.less";
const BgElement = Element.BgElement;

export default function ({ bannerList }) {
  const [_, setRoute] = useRoute();

  return (
    <BannerAnim
      prefixCls="banner-user component-banner-wrap"
      autoPlay
      autoPlaySpeed={8000}
      type="across"
      arrow={false}
      delay={300}
    >
      {bannerList.map((item, index) => (
        <Element prefixCls="banner-user-elem" key={`${index}`}>
           {item.video ? (
            <BgElement
              component="video"
              componentProps={{
                className: "video",
                autoPlay: true,
                muted: true,
                poster: video_banner,
                src: item.video,
              }}
              className="video_size"
            />
          ) : (
            <BgElement
              key="bg"
              className="bg"
              style={{ backgroundImage: "url(" + item.image + ")" }}
            />
          )}
          <TweenOne
            className="title box-container"
            animation={{ y: 30, opacity: 0, type: "from" }}
          >
            <FontText
              key="title"
              theme="light"
              size={40}
              lineHeight="60px"
              fontWeight="bold"
              top={187}
              textAlign="center"
            >
              {item.title}
            </FontText>
          </TweenOne>
          <TweenOne
            className="box-container"
            animation={{ y: 30, opacity: 0, type: "from", delay: 100 }}
          >
            <FontText
              key="text"
              theme="light"
              top={12}
              size={18}
              // width={548}
              color={85}
              lineHeight="25px"
              content={item.subTitle}
              textAlign="center"
            />
          </TweenOne>
          {!item.btn && (
            <TweenOne
              className="box-container"
              animation={{ y: 30, opacity: 0, type: "from", delay: 100 }}
            >
              <div className="block_item"></div>
            </TweenOne>
          )}
          {item.btn && (
            <TweenOne
              className="box-container"
              style={{ marginTop: 48, with: 1200 }}
              animation={{ y: 30, opacity: 0, type: "from", delay: 100 }}
            >
              <div className="btn">
                <Button
                  key="btn"
                  type="primary"
                  onClick={() => setRoute(item.url)}
                  style={{ minWidth: 180, height: 54, background: "#4E80F8" }}
                >
                  {item.btn}
                  <RightOutlined />
                </Button>
              </div>
            </TweenOne>
          )}
        </Element>
      ))}
    </BannerAnim>
  );
}

2、设计需要map操作的数据结构

export const bannerList = [
  {
    id: "1",
    btn: "",
    title: "我是banner1,是视频",
    subTitle: "banner下面的一小行简介",
    video: require("@/assets/images/home/banner_video.mp4"),
  },
  {
    id: "3",
    btn: "banner2有按钮",
    title: "我是banner2,是图片",
    subTitle: "banner下面的一小行简介",
    image: require("@/assets/images/home/banner2.jpg"),
  },
  {
    id: "2",
    btn: "banner3有按钮",
    title: "我是banner3,是图片",
    subTitle: "banner下面的一小行简介",
    image: require("@/assets/images/home/banner3.jpeg"),
  },
];
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

椰卤工程师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值