前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >bootstrap v4 toast轻提示正确用法

bootstrap v4 toast轻提示正确用法

作者头像
hotqin888
发布2020-07-27 16:00:17
1.7K0
发布2020-07-27 16:00:17
举报
文章被收录于专栏:hotqin888的专栏hotqin888的专栏

用vue和小程序开发的同学会感到里面的toast轻提示很好,可惜bootstrap到4以上才支持,而它的帮助里写的代码感觉都是“扯淡”的,根本用不起来效果。

轻提示首先是不影响页面布局,像弹窗一样,设置延迟的时间后会自动消失。

下图是vue的。——新建成功那个就是轻提示了。

所以这里有标准的用法。

https://github.com/Script47/Toast

https://www.kaiu.net/script/content/3154

https://www.kaiu.net/script/show/3154

注意,git上代码在不断更新,并且example也是在更新,请以上面第一为准哦。

首先要引用git上toast.css和js,以及bootstrap.js和css的4.0版本以上,然后将example代码拷贝过来使用即可。

代码语言:javascript
复制
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
  <script src="../dist/toast.min.js"></script>
  <script>
  const TYPES = ['info', 'warning', 'success', 'error'],
    TITLES = {
      'info': 'Notice!',
      'success': 'Awesome!',
      'warning': 'Watch Out!',
      'error': 'Doh!'
    },
    CONTENT = {
      'info': 'Hello, world! This is a toast message.',
      'success': 'The action has been completed.',
      'warning': 'It\'s all about to go wrong',
      'error': 'It all went wrong.'
    },
    POSITION = ['top-right', 'top-left', 'top-center', 'bottom-right', 'bottom-left', 'bottom-center'];

  $.toastDefaults.position = 'bottom-center';
  $.toastDefaults.dismissible = true;
  $.toastDefaults.stackable = true;
  $.toastDefaults.pauseDelayOnHover = true;

  $('.snack').click(function() {
    var type = TYPES[Math.floor(Math.random() * TYPES.length)],
      content = CONTENT[type];

    $.snack(type, content);
  });

  $('.toast-btn').click(function() {
    var rng = Math.floor(Math.random() * 2) + 1,
      type = TYPES[Math.floor(Math.random() * TYPES.length)],
      title = TITLES[type],
      content = CONTENT[type];

    if (rng === 1) {
      $.toast({
        type: type,
        title: title,
        subtitle: '11 mins ago',
        content: content,
        delay: 5000
      });
    } else {
      $.toast({
        type: type,
        title: title,
        subtitle: '11 mins ago',
        content: content,
        delay: 5000,
        img: {
          src: 'https://via.placeholder.com/20',
          alt: 'Image'
        }
      });
    }
  });
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-07-25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云开发 CloudBase
云开发(Tencent CloudBase,TCB)是腾讯云提供的云原生一体化开发环境和工具平台,为200万+企业和开发者提供高可用、自动弹性扩缩的后端云服务,可用于云端一体化开发多种端应用(小程序、公众号、Web 应用等),避免了应用开发过程中繁琐的服务器搭建及运维,开发者可以专注于业务逻辑的实现,开发门槛更低,效率更高。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档