GVKun编程网logo

tailwindCSS - 如何在轮播幻灯片上添加按钮?(幻灯片轮播怎么设置)

15

如果您对tailwindCSS-如何在轮播幻灯片上添加按钮?和幻灯片轮播怎么设置感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解tailwindCSS-如何在轮播幻灯片上添加按钮?的各种细节,并

如果您对tailwindCSS - 如何在轮播幻灯片上添加按钮?幻灯片轮播怎么设置感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解tailwindCSS - 如何在轮播幻灯片上添加按钮?的各种细节,并对幻灯片轮播怎么设置进行深入的分析,此外还有关于2023 告别 CSS 预处理工具,彻底拥抱 TailwindCSS、@tailwindcss/jit 在 postcss.config.js 中配置时不起作用 这是我的依赖项,我已经安装了@tailwindcss/jit、android – 如何在图像按钮上添加按钮标题、css+js如何在幻灯片上添加文字?实现幻灯片的旋转切换(附代码)的实用技巧。

本文目录一览:

tailwindCSS - 如何在轮播幻灯片上添加按钮?(幻灯片轮播怎么设置)

tailwindCSS - 如何在轮播幻灯片上添加按钮?(幻灯片轮播怎么设置)

如何解决tailwindCSS - 如何在轮播幻灯片上添加按钮??

我一直在玩这个轮播created by Tom Smedley。他使用 AlpineJS 和 TailwindCSS。这真是一个华丽的设计。

我重新编辑了上面链接的代码笔以适合我的问题。

Here is my version of the sandbox which replicates the problem

我为每个轮播幻灯片添加了一个 LEARN MORE 按钮,但是该按钮隐藏在 <h1> 内容标题后面。为了测试,我在按钮上添加了悬停效果来改变颜色,但是按钮无法悬停。

  • 如何将按钮固定在 <h1> 文本的前面并使其位于 <h1> 标题的左下角?我可以通过设置按钮边距来解决这个问题,但由于每张幻灯片上文本长度的性质,这不是一个好的解决方案。

以下是我想要达到的目标的屏幕截图:

enter image description here

我不在乎按钮是否在幻灯片之外(由于 h1 文本长度),我只希望它位于标题文本的左下方。

我尝试了很多使用 flex、网格等的尝试......但我一直在挣扎。谢谢你的帮助。

解决方法

将所有 <button> 标签放在 <h1> 标签中,并插入以下 CSS:

 h1 > button {
      position: absolute !important;
      left: 0;
      top: 100%;
      font-size: 1rem !important;
    }

我还包含了一个片段。以全屏模式运行:

function carousel() {
        return {
            active: 0,init() {
                var flkty = new Flickity(this.$refs.carousel,{
                    wrapAround: true,});
                flkty.on("change",(i) => (this.active = i));
            },};
    }

    function carouselFilter() {
        return {
            active: 0,changeActive(i) {
                this.active = i;

                this.$nextTick(() => {
                    let flkty = Flickity.data(
                        this.$el.querySelectorAll(".carousel")[i]
                    );
                    flkty.resize();

                    console.log(flkty);
                });
            },};
    }
.flickity-viewport {
      height: 500px !important;
    }

    h1 > button {
      position: absolute !important;
      left: 0;
      top: 100%;
      font-size: 1rem !important;
      pointer-events: auto; /* This is part of EDIT 2 */
    }
    
<html>

<head></head>
<script src="https://unpkg.com/flickity@2/dist/flickity.pkgd.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/alpinejs/alpine@v2.x.x/dist/alpine.min.js"></script>
<link rel="stylesheet" href="https://unpkg.com/flickity@2/dist/flickity.min.css">
<link rel="stylesheet" href="https://unpkg.com/tailwindcss@1.2.0/dist/tailwind.min.css">

<body>
    <mainx-data="carouselFilter()"
    >
      <div>
        <div>
          <a:href="#"
            @click.prevent="changeActive(0)"
            >Lorem Ipsum</a
          >
        </div>

        <divx-show="active == 0"
          x-transition:enter="transition ease-out duration-300"
          x-transition:enter-start="opacity-0 transform scale-90"
          x-transition:enter-end="opacity-100 transform scale-100"
          x-transition:leave="transition ease-in duration-300"
          x-transition:leave-start="opacity-100 transform scale-100"
          x-transition:leave-end="opacity-0 transform scale-90"
        >
          <divx-data="carousel()"
            x-init="init()"
          >
            <div>
              <h1x-show="active == 0"
                x-transition:enter="transition ease-out duration-300"
                x-transition:enter-start="opacity-0 transform translate-y-12"
                x-transition:enter-end="opacity-100 transform translate-y-0"
                x-transition:leave="transition ease-out duration-300"
                x-transition:leave-start="opacity-100 transform translate-y-0"
                x-transition:leave-end="opacity-0 transform -translate-y-12"
              >
                Lorem Ipsum is simply dummy text of the printing and typesetting
                industry. Lorem Ipsum has been the industry''s standard dummy
                text ever since the 1500s
                <buttonx-show="active == 0"
                >
                  LEARN MORE
                </button>
              </h1>

              <h1x-show="active == 1"
                x-transition:enter="transition ease-out duration-300"
                x-transition:enter-start="opacity-0 transform translate-y-12"
                x-transition:enter-end="opacity-100 transform translate-y-0"
                x-transition:leave="transition ease-out duration-300"
                x-transition:leave-start="opacity-100 transform translate-y-0"
                x-transition:leave-end="opacity-0 transform -translate-y-12"
              >
                It is a long established fact that a reader will be distracted
                by the readable content of a page when looking at its layout.
                <buttonx-show="active == 1"
                >
                  LEARN MORE
                </button>
              </h1>
              <h1x-show="active == 2"
                x-transition:enter="transition ease-out duration-300"
                x-transition:enter-start="opacity-0 transform translate-y-12"
                x-transition:enter-end="opacity-100 transform translate-y-0"
                x-transition:leave="transition ease-out duration-300"
                x-transition:leave-start="opacity-100 transform translate-y-0"
                x-transition:leave-end="opacity-0 transform -translate-y-12"
              >
                There are many variations of passages of Lorem Ipsum available
                <buttonx-show="active == 2"
                >
                  LEARN MORE
                </button>
              </h1>
              <h1x-show="active == 3"
                x-transition:enter="transition ease-out duration-300"
                x-transition:enter-start="opacity-0 transform translate-y-12"
                x-transition:enter-end="opacity-100 transform translate-y-0"
                x-transition:leave="transition ease-out duration-300"
                x-transition:leave-start="opacity-100 transform translate-y-0"
                x-transition:leave-end="opacity-0 transform -translate-y-12"
              >
                but the majority have suffered alteration in some form
                <buttonx-show="active == 3"
                >
                  LEARN MORE
                </button>
              </h1>
            </div>

            <divx-ref="carousel">
              <div>
                <img
                  src="https://images.unsplash.com/photo-1581375221876-8f287f7cd2cc?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=770&q=80"
                  loading="lazy"
                />
              </div>
              <div>
                <img
                  src="https://images.unsplash.com/photo-1581375279144-bb3b381c7046?ixlib=rb-1.2.1&auto=format&fit=crop&w=770&q=80"
                  loading="lazy"
                />
              </div>
              <div>
                <img
                  src="https://images.unsplash.com/photo-1581375303816-4a17124934f7?ixlib=rb-1.2.1&auto=format&fit=crop&w=770&q=80"
                  loading="lazy"
                />
              </div>
              <div>
                <img
                  src="https://images.unsplash.com/photo-1494253109108-2e30c049369b?ixlib=rb-1.2.1&auto=format&fit=crop&w=770&q=80"
                  loading="lazy"
                />
              </div>
            </div>
          </div>
        </div>

        <divx-show="active == 1"
          x-transition:enter="transition ease-out duration-300"
          x-transition:enter-start="opacity-0 transform scale-90"
          x-transition:enter-end="opacity-100 transform scale-100"
          x-transition:leave="transition ease-in duration-300"
          x-transition:leave-start="opacity-100 transform scale-100"
          x-transition:leave-end="opacity-0 transform scale-90"
        ></div>
      </div>
    </main>
</body>

</html>

您无法对按钮应用悬停效果的原因是某个 pointer-events 属性已设置为 none,在包含 {{1} } 和 <h1> 标签。这是因为如果不这样设置,应用于背景的拖动功能将不起作用。因此,您可以选择单击按钮并突出显示标题文本,或者可以拖动和滚动背景。

如果您删除 pointer-events-none 类,您会注意到您的悬停效果在按钮上起作用。

编辑:

好的,所以我们进入了第 2 部分。基本上我们需要做的是将按钮提升到标题所在的级别。当这个页面在 DOM 中呈现时,您可以使用 DevTools 检查元素。我从 DOM 中复制了按钮元素,并将它们与所有 <button> 标记放在同一个容器中。所以现在你有 2 组按钮。这是一组的样子:

<h1>

稍微翻阅 Flickity 文档,您会发现按钮是 optional,用于渲染。同样,由于我们不会直接与 Flickity 实例交互,因此我在初始化时禁用了拖动功能。这看起来像这样:

<buttontype="button" aria-label="Previous" onclick="slide(''previous'')">
  <svgviewBox="0 0 100 100">
    <path d="M 10,50 L 60,100 L 70,90 L 30,50  L 70,10 L 60,0 Z"></path>
  </svg>
</button>
<buttontype="button" aria-label="Next" onclick="slide(''next'')">
  <svgviewBox="0 0 100 100">
    <path d="M 10,0 Z"transform="translate(100,100) rotate(180) "></path>
  </svg>
</button>

由于我们添加的新按钮基本上是 Flickity 使用的按钮,所以我们真的不需要太担心样式。只需要稍微修改一下。为了减少意外修改实际 CSS 的机会,我添加了一个类 function carousel() { return { active: 0,init() { var flkty = new Flickity(this.$refs.carousel,{ wrapAround: true,draggable: false,// This is new prevNextButtons: false // This is new }); flkty.on("change",(i) => (this.active = i)); } }; }

附加样式:

custom-flickity-button

最后,我们需要为新按钮提供一些点击功能:

  .custom-flickity-button {
    /* 21 is the threshold. Below this,it will be under the h1 text */
    z-index: 25;
  }

  .custom-flickity-button:focus {
    outline: none;
    box-shadow: none;
  }

  /* 
  If you make h1 relative,then this padding will apply. 
  But that was causing some issues while transitioning. 
  I''m leaving this for you to explore. 
  */
  #text-container {
    padding: 0 70px;
  }

我不太熟悉使用 Alpine 和 Tailwind,因此您可能会修改此代码以适应它。但无论如何,这就是你所要求的。带有交互式滑块按钮的交互式了解详情按钮。

// This is to make the new buttons slide. You can modify the logic to work with multiple carousels,as you''ve done above
  function slide(value) {
    let flkty = Flickity.data(document.querySelector(''.carousel''))
    if (value == ''next'') {
      flkty.next()
    } else if (value == ''previous'') {
      flkty.previous()
    }
  }
function carousel() {
        return {
            active: 0,};
    }
    
      // This is to make the new buttons slide. You can modify the logic to work with multiple carousels,as you''ve done above
  function slide(value) {
    let flkty = Flickity.data(document.querySelector(''.carousel''))
    if (value == ''next'') {
      flkty.next()
    } else if (value == ''previous'') {
      flkty.previous()
    }
  }
.flickity-viewport {
      height: 500px !important;
    }

    h1 > button {
      position: absolute !important;
      left: 0;
      top: 100%;
      font-size: 1rem !important;
    }
    
    .custom-flickity-button {
    /* 21 is the threshold. Below this,it will be under the h1 text */
    z-index: 1
  }

  .custom-flickity-button:focus {
    outline: none;
    box-shadow: none;
    border: none;
  }
      /* 
  If you make h1 relative,then this padding will apply. 
  But that was causing some issues while transitioning. 
  I''m leaving this for you to explore. 
  */
  #text-container {
    padding: 0 70px;
  }

PS:出于某种原因,<html> <head></head> <script src="https://unpkg.com/flickity@2/dist/flickity.pkgd.min.js"></script> <script src="https://cdn.jsdelivr.net/gh/alpinejs/alpine@v2.x.x/dist/alpine.min.js"></script> <link rel="stylesheet" href="https://unpkg.com/flickity@2/dist/flickity.min.css"> <link rel="stylesheet" href="https://unpkg.com/tailwindcss@1.2.0/dist/tailwind.min.css"> <body> <mainx-data="carouselFilter()"> <div> <div> <a:href="#" @click.prevent="changeActive(0)">Lorem Ipsum</a> </div> <divx-show="active == 0" x-transition:enter="transition ease-out duration-300" x-transition:enter-start="opacity-0 transform scale-90" x-transition:enter-end="opacity-100 transform scale-100" x-transition:leave="transition ease-in duration-300" x-transition:leave-start="opacity-100 transform scale-100" x-transition:leave-end="opacity-0 transform scale-90"> <divx-data="carousel()" x-init="init()"> <divid="text-container"> <buttontype="button" aria-label="Previous" onclick="slide(''previous'')"><svgviewBox="0 0 100 100"> <path d="M 10,0 Z"></path> </svg></button> <buttontype="button" aria-label="Next" onclick="slide(''next'')"><svgviewBox="0 0 100 100"> <path d="M 10,0 Z"transform="translate(100,100) rotate(180) "></path> </svg></button> <h1x-show="active == 0" x-transition:enter="transition ease-out duration-300" x-transition:enter-start="opacity-0 transform translate-y-12" x-transition:enter-end="opacity-100 transform translate-y-0" x-transition:leave="transition ease-out duration-300" x-transition:leave-start="opacity-100 transform translate-y-0" x-transition:leave-end="opacity-0 transform -translate-y-12"> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry''s standard dummy text ever since the 1500s <buttonx-show="active == 0"> LEARN MORE </button> </h1> <h1x-show="active == 1" x-transition:enter="transition ease-out duration-300" x-transition:enter-start="opacity-0 transform translate-y-12" x-transition:enter-end="opacity-100 transform translate-y-0" x-transition:leave="transition ease-out duration-300" x-transition:leave-start="opacity-100 transform translate-y-0" x-transition:leave-end="opacity-0 transform -translate-y-12"> It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. <buttonx-show="active == 1"> LEARN MORE </button> </h1> <h1x-show="active == 2" x-transition:enter="transition ease-out duration-300" x-transition:enter-start="opacity-0 transform translate-y-12" x-transition:enter-end="opacity-100 transform translate-y-0" x-transition:leave="transition ease-out duration-300" x-transition:leave-start="opacity-100 transform translate-y-0" x-transition:leave-end="opacity-0 transform -translate-y-12"> There are many variations of passages of Lorem Ipsum available <buttonx-show="active == 2"> LEARN MORE </button> </h1> <h1x-show="active == 3" x-transition:enter="transition ease-out duration-300" x-transition:enter-start="opacity-0 transform translate-y-12" x-transition:enter-end="opacity-100 transform translate-y-0" x-transition:leave="transition ease-out duration-300" x-transition:leave-start="opacity-100 transform translate-y-0" x-transition:leave-end="opacity-0 transform -translate-y-12"> but the majority have suffered alteration in some form <buttonx-show="active == 3"> LEARN MORE </button> </h1> </div> <divx-ref="carousel"> <div> <img src="https://images.unsplash.com/photo-1581375221876-8f287f7cd2cc?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=770&q=80" loading="lazy" /> </div> <div> <img src="https://images.unsplash.com/photo-1581375279144-bb3b381c7046?ixlib=rb-1.2.1&auto=format&fit=crop&w=770&q=80" loading="lazy" /> </div> <div> <img src="https://images.unsplash.com/photo-1581375303816-4a17124934f7?ixlib=rb-1.2.1&auto=format&fit=crop&w=770&q=80" loading="lazy" /> </div> <div> <img src="https://images.unsplash.com/photo-1494253109108-2e30c049369b?ixlib=rb-1.2.1&auto=format&fit=crop&w=770&q=80" loading="lazy" /> </div> </div> </div> </div> </div> <divx-show="active == 1" x-transition:enter="transition ease-out duration-300" x-transition:enter-start="opacity-0 transform scale-90" x-transition:enter-end="opacity-100 transform scale-100" x-transition:leave="transition ease-in duration-300" x-transition:leave-start="opacity-100 transform scale-100" x-transition:leave-end="opacity-0 transform scale-90" ></div> </main> </body> </html> 仍在代码段中显示蓝色框阴影。它没有正常显示,所以你可能想看看它。

编辑 2:

嘿!刚刚对指针事件属性做了一些阅读,结果证明如果你将 .custom-flickity-button:focus 设置为一个元素,但给它的任何子元素赋值 pointer-events: none,父元素将不会阻止触发孩子的指针事件。

基本上这意味着我在编辑 1 中所做的一切都是不必要的。您需要做的就是在我的原始答案中的按钮中添加一行额外的 CSS -> pointer-events: auto,您将拥有可用的按钮和可拖动的轮播!

这个不错!它解决了你的问题,你和我今天都学到了一些新东西! :P

我已在我的原始代码段中进行了此更改,因此您可以在此处查看。至于编辑 1,我暂时保持原样。可能会在其他时间帮助别人,谁知道呢。

2023 告别 CSS 预处理工具,彻底拥抱 TailwindCSS

2023 告别 CSS 预处理工具,彻底拥抱 TailwindCSS

CSS 是声明式语言,很简单,很好学,但是写起来很累,所有东西都要写出来才能生效。复用方面更是无从下手,虽然大家都在不断总结,但始终没能找到足够好用的方案,可以有效改善 CSS 开发。

于是我们只好把视线转出 CSS 之外,转投 CSS 预处理工具,Less、SASS(SCSS)、Stylus,引入种种 CSS 不具备的功能,帮助我们改进开发体验。比如嵌套、函数、循环、条件,等等。然而如果你细心观察,实际上,这几个工具最近 5、6 年都没怎么更新(我说的是功能性),因为该有的都有了,甚至很稳定;其它来自于 CSS 的改进,几乎跟它们没什么关系,也不用更新。

最近几年,随着 CSS 发展,一些新特性逐步引入,我觉得这些工具越来越难用,它们能带来的好处已经无法掩盖它们所造成的问题。是时候告别 CSS 预处理工具了,就像我们当年告别 jQuery 一样。

为什么说预处理工具落后?

我把理由分成三大类:

预处理工具的问题

  • 对 CSS 函数兼容性不好,尤其是 rgba()hsl() 这些常用的颜色函数
  • 数值类型转换,有不符合预期的行为,比如 Stylus 实现 content:5

CSS 的改进

  • CSS 拥有越来越多的函数,可以直接进行计算,比如前面提到的颜色;还有 calc() 来完成基础数学计算
  • CSS 变量非常好用,可以大大改进编程体验,配合各种 JS 框架,我们可以更容易的把数学逻辑和显示效果绑定在一起
  • CSS Houdini 可以实现很多新功能,即使不深入使用(JS 部分),也有好用的自定义属性
  • CSS 也开始从预处理工具吸收营养,比如近期的嵌套功能已经开始被整合,未来我们可以直接使用

预处理工具无法跟进的问题

  • 很多缩写、复合属性无法处理,比如 background-image、box-shadow 等,都支持多属性共同生效,预处理工具擅长的循环、条件、函数无法提供帮助。
  • 预处理,顾名思义,发生在生产之前。实际上,网页在实际浏览时,会有很多因素影响到渲染结果,比如分辨率、dark mode 等。预处理工具对这些需求也没有改进。

替代方案

我目前的替代方案基于 TailwindCSS,所以自然包含 PostCSS、AutoPrefixer 等工具。然后用 postcss-import 实现自动导入和模块化;使用 tailwindcss/nesting 实现嵌套。

为什么选用 TailwindCSS?首先,实际开发中,不管使用什么前端框架,我们都需要大量原子化的胶水样式,比如调整间距、改变字号、给容器添加一些边框、圆角、阴影等。这些样式如果都手写,工作量并不小;学习不同的样式名也是负担;以及最重要的,CSS 优先级问题。使用 TailwindCSS 就都能很好解决。

TailwindCSS 不仅包含一大堆原子化样式,自身也是个完整且优秀的 CSS 编译器。它包含 reset,提供一组全局通用的 CSS 变量;它可以从各种文件里把我们用到的样式提取出来,构建后生成的 CSS 里只有我们要用到样式,不会有多余的;它会分析我们对样式的使用,合理的调整样式顺序,保证样式能正确生效。使用 TailwindCSS 可以节省很多时间。

它还自带若干插件,比如解决嵌套的 tailwindcss/nesting,支持内容类元素的的 @tailwindcss/typography 等。使用这些插件也可以帮我们节省很多时间。

最后,TailwindCSS 的生态不断成长,我们的选择范围越来越宽:HeadlessUI、DaisyUI、付费的 Tailwind UI 等。方便我们从产品生命周期的任意阶段开始集成。

推荐项目配置

启动项目的时候,安装依赖。包含 PostCSS + AutoPrefixer、TailwindCSS 和 DaisyUI。前者提供 CSS 处理框架,包含自动导入 css 和嵌套功能;后两者提供可见的 UI。

pnpm i postcss postcss-import tailwindcss autoprefixer daisyui -D

自动初始化配置,-p 会自动生成 PostCSS 配置:

pnpm tailwindcss init -p

调整 postcss.config.js,启用 postcss-importtailwindcss/nesting。目前我们常用的嵌套规则和 CSS 规范略有区别,不过无所谓,规范也没确定,所以这样就足够了。

// postcss.config.js
module.exports = {
  plugins: {
    ''postcss-import'': {},
    ''tailwindcss/nesting'': {},
    tailwindcss: {},
    autoprefixer: {},
  },
}

然后调整 tailwind.config.js

// tailwind.config.js
const DaisyUI = require(''daisyui'');
// 这个插件可以帮我们处理文档类内容,我建议常用
const Typography = require(''@tailwindcss/typography'');

module.exports = {
  // 从以下文件查找用到的样式
  content: [
    ''./index.html'',
    ''./src/**/*.{js,ts,jsx,tsx,vue}'',
  ],
  theme: {
    extend: {
      // 扩充 TailwindCSS 没有包含的样式
    },
  },
  plugins: [
    DaisyUI,
    Typography,
  ],
  daisyui: {
    themes: [{
      // 只构建一个主题: luxury,并覆盖其中的两个属性
      luxury: {
        ...require(''daisyui/src/colors/themes'')[ ''[data-theme=luxury]'' ],
        primary: ''#FFA028'',
        ''--bc'': ''0 0% 87.5%'',
      },
    }],
  },
}

然后,创建样式入口 main.css。其它样式可以如常写在这个文件里,不过如果要 @import 其它 CSS 文件,就要进行一些调整。具体可以看官方文档。

// main.css
@tailwind base;
@tailwind components;
@tailwind utilities;

然后在入口文件引用 main.css 即可:

// main.js
import ''./main.css'';

至此,新项目配置完成,可以照常开发了。

下期预告

这次我先分享了整体思路:用新的工具链替代预处理工具,保证已有的功能不缺失。那么下期分享的内容就是使用新的 CSS 特性,更好的完成开发。


如果你对新 CSS 感兴趣,对预处理工具和新工具链有兴趣和疑问,欢迎留言讨论。如果本文对你有启发,也请帮我点赞分享,谢谢。


本文参与了SegmentFault 思否写作挑战赛,欢迎正在阅读的你也加入。

@tailwindcss/jit 在 postcss.config.js 中配置时不起作用 这是我的依赖项,我已经安装了@tailwindcss/jit

@tailwindcss/jit 在 postcss.config.js 中配置时不起作用 这是我的依赖项,我已经安装了@tailwindcss/jit

这是您安装的 Tailwind JIT 版本 (0.1.3) 中的一个已知错误。由于文件路径中的空格导致编译失败。

https://github.com/tailwindlabs/tailwindcss-jit/issues/22

已发布修复程序。您需要做的就是更新依赖项,它应该按预期构建。

android – 如何在图像按钮上添加按钮标题

android – 如何在图像按钮上添加按钮标题

我将在我的应用程序中使用图像按钮我需要在按钮上显示按钮标题我正在使用此代码但它没有显示标题

<ImageButton 
    android:id="@+id/try_button"
    android:background="@drawable/custom_button"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_margin="10dp"
    android:text="@string/trybutton_caption" />

我怎样才能做到这一点?

谢谢.

解决方法

使用Button而不是ImageButton.

有用:

android:drawabletop="@drawable/image"
android:text="@string/text"

仅使用drawabletop,将在文本上方绘制drawable.

css+js如何在幻灯片上添加文字?实现幻灯片的旋转切换(附代码)

css+js如何在幻灯片上添加文字?实现幻灯片的旋转切换(附代码)

本篇文章给大家带来的内容是介绍css+js如何在幻灯片上添加文字?实现幻灯片的旋转切换(附代码)。有一定的参考价值,有需要的朋友可以参考一下,希望对你们有所帮助。

在之前的文章【css如何实现幻灯片效果?幻灯片的实现方法】中介绍了实现淡入淡出幻灯片的实现方法,本篇文章就在其基础上去解释如何在幻灯片上添加文字,可以随着幻灯片切换不同文字;以及把淡入淡出效果转换成旋转木马的切换效果的方法(附代码)。

下面我们就来一步步实现效果(可以去之前的文章获取实现幻灯片效果的代码)。

5、给幻灯片添加文本文字

有很多方法可以解决这个问题,但也许最简单的方法是在链接中添加一些标题属性,并使用CSS在图像上显示它们:

立即学习“前端免费学习笔记(深入)”;

html代码:

<div id="stage">
	<a href="img/1.jpg" title="图片1">@@##@@</a>
	<a href="img/2.jpg" title="图片2">@@##@@</a>
	<a href="img/3.jpg" title="图片3">@@##@@</a>
	<a href="img/4.jpg" title="图片4">@@##@@</a>
	<a href="img/5.jpg" title="图片5">@@##@@</a>
	<a href="img/6.jpg" title="图片6">@@##@@</a>
	<a href="img/7.jpg" title="图片7">@@##@@</a>
	<a href="img/8.jpg" title="图片8">@@##@@</a>
</div>
登录后复制

css代码:

#stage a::after {
    position: absolute;
    left: 11px;
    bottom: 11px;
    padding: 2px 0;
    width: calc(100% - 22px);
    background: rgba(0,0,0,0.5);
    text-align: center;
    content: attr(title);
    font-size: 1.1em;
    color: #fff;
  }	
登录后复制

除了为我们的链接添加title属性之外没有其他任何变化,我们现在已经开始了基本的CTA幻灯片放映,甚至可以将其转换为对关键帧进行一些调整的轮播。

效果图:

css+js如何在幻灯片上添加文字?实现幻灯片的旋转切换(附代码)css+js如何在幻灯片上添加文字?实现幻灯片的旋转切换(附代码)

所以,如果你必须有一个主页幻灯片来取悦那些权力,那就考虑放弃jQuery,就像上面那样。如果您有任何反馈或问题,请使用下面的按钮告诉我们。

6、转换为旋转木马切换效果

效果图(静态的,大家可以自己编译运行,查看效果):

css+js如何在幻灯片上添加文字?实现幻灯片的旋转切换(附代码)

附上css+js代码(html代码如上):

css代码:

#stage {
    margin: 1em auto;
    width: 640px;
    height: 400px;
    border: 10px solid #000;
    overflow: hidden;
  }
  #stage a {
    position: relative;
    display: inline-block;
  }
 #stage a::after {
    position: absolute;
    left: 11px;
    bottom: 11px;
    padding: 2px 0;
    width: calc(100% - 22px);
    background: rgba(0,0,0,0.5);
    text-align: center;
    content: attr(title);
    font-size: 1.1em;
    color: #fff;
  }

  #stage a:nth-of-type(2) {
    left: 640px;
    top: -50%;
    animation-name: slider;
    animation-delay: 4s;
    animation-duration: 1s;
    animation-timing-function: cubic-bezier(0,1.5,0.5,1);
  }
  #stage a:nth-of-type(n+3) {
    display: none;
  }

  @keyframes slider {
    from { transform: translateY(-50%) rotate(30deg); left: 360px; }
    to   { transform: translateY(-50%); left: 0px; }
  }
登录后复制

js代码:

window.addEventListener("DOMContentLoaded", function(e) {

    var stage = document.getElementById("stage");
    var slideComplete = function(e) { stage.appendChild(arr[0]); };
    var arr = stage.getElementsByTagName("a");
    for(var i=0; i < arr.length; i++) {
      arr[i].addEventListener("animationend", slideComplete, false);
    }

  }, false);
登录后复制

大功告成,大家可以自己动手编译看看效果!

总结:以上就是本篇文章所介绍的全部内容,希望能对大家的学习有所帮助。

css+js如何在幻灯片上添加文字?实现幻灯片的旋转切换(附代码)css+js如何在幻灯片上添加文字?实现幻灯片的旋转切换(附代码)css+js如何在幻灯片上添加文字?实现幻灯片的旋转切换(附代码)css+js如何在幻灯片上添加文字?实现幻灯片的旋转切换(附代码)css+js如何在幻灯片上添加文字?实现幻灯片的旋转切换(附代码)1541411016286356.jpg1541411022433022.jpg3.jpg

以上就是css+js如何在幻灯片上添加文字?实现幻灯片的旋转切换(附代码)的详细内容,更多请关注php中文网其它相关文章!

关于tailwindCSS - 如何在轮播幻灯片上添加按钮?幻灯片轮播怎么设置的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于2023 告别 CSS 预处理工具,彻底拥抱 TailwindCSS、@tailwindcss/jit 在 postcss.config.js 中配置时不起作用 这是我的依赖项,我已经安装了@tailwindcss/jit、android – 如何在图像按钮上添加按钮标题、css+js如何在幻灯片上添加文字?实现幻灯片的旋转切换(附代码)等相关知识的信息别忘了在本站进行查找喔。

本文标签:

上一篇Express 加载无穷大并且在使用 Express body Parser 时不起作用

下一篇如何用sass动态改变页面的方向?(sass 动态变量)