使用 html、css 切换每个 div 标签的可见性的最简单幻灯片

发布于 2024-10-18 12:05:18 字数 666 浏览 7 评论 0原文

我有几个包含简单 html/css 内容的 div 标签,我需要一种简单的方法让查看者单击链接“幻灯片 1”,该幻灯片的第一个 div 标签变得可见,然后他们单击幻灯片 2,第二个 div 标签是可见,等等。

我尝试在 Dreamweaver 中使用时间线,但这似乎太过分了,而且太复杂了。引入整个 YUI 库也是如此。我们只需要一个基本的、强力的方法,即在单击链接时使其可见/不可见。

所以设置是

     <LINK_SLIDE1>  <LINK_SLIDE2>  <LINK_SLIDE3>
<div class="slide" id="slide1">
  <p>Welcome etc etc etc</p>
</div
<div class="slide" id="slide2">
  <p>Overview etc etc etc</p>
</div
<div class="slide" id="slide3">
  <p>Summary etc etc</p>
</div

我正在寻找一些关于如何实现链接来打开/关闭每个 div 标签的可见性的提示。我担心的一个问题是,如果用户没有启用 JavaScript,我们也可以处理这种情况吗?

I have several div tags containing simple html/css content and I need a simple way for a viewer to click a link 'Slide 1' and the first div tag for that slide become visible, then they click slide 2 and the second div tag is visible, and so on.

i tried using timelines in Dreamweaver but that seems like overkill and far too complex. So is pulling in the whole YUI library. Just a basic, brute force, make visible/invisible when a link is clicked is all we need.

so the setup is

     <LINK_SLIDE1>  <LINK_SLIDE2>  <LINK_SLIDE3>
<div class="slide" id="slide1">
  <p>Welcome etc etc etc</p>
</div
<div class="slide" id="slide2">
  <p>Overview etc etc etc</p>
</div
<div class="slide" id="slide3">
  <p>Summary etc etc</p>
</div

And I'm looking for some tips on how to implement the LINKs to toggle the visiblity of each div tag on/off. One problem I'm worried about is if a user doesnt have javascript enabled, can we handle that case too.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

万水千山粽是情ミ 2024-10-25 12:05:18

用户单击链接时的基本流程应该是:

  1. 隐藏所有带有“slide”类的 div。
  2. 显示 ID 为“slideX”的 div(其中 X 由单击的链接确定)。

您可以通过向每个链接添加事件侦听器,然后确定单击了哪个链接以及显示哪张幻灯片来实现此目的。对于此类问题,使用链接中的 rel 属性是存储此信息的快速方法。

这是一个不使用任何框架的示例,我还没有对其进行测试。仅举一个例子。

var slides = document.getElementsByClassName('slide');
function showSlide(e) {
    var toShow = e.target.getAttribute('rel');
    for (var i = 0, len = slides.length; i < len; i++) {
        slides[i].style.display = 'none';
    }
    document.getElementById(toShow).style.display = 'block';
    e.preventDefault();
    return false;
}

var links = document.getElementsByClassName('slide-link');

for (var i = 0, len = links.length; i < len; i++) {
    links[i].addEventListener('click', showSlide, false);
}

以及与之配套的 HTML 示例:

<a href="#" class="slide-link" rel="slide1">Welcome</a>
<a href="#" class="slide-link" rel="slide2">Overview</a>

<div class="slide" id="slide1">
    <p>Welcome etc etc etc</p>
</div>
<div class="slide" id="slide2">
    <p>Overview etc etc etc</p>
</div>

希望这能让您走上正轨。

The basic flow when the user clicks the link should be:

  1. Hide all divs with the class 'slide'.
  2. Show the div with the id "slideX" (where X is determined by the link clicked).

You can do this by adding event listeners to each link, then figuring out by which link was clicked, which slide to show. For problems like this using the rel attribute in the link is a quick way to store this info.

Here's an example using no framework whatsoever, and I haven't tested it. Just for an example.

var slides = document.getElementsByClassName('slide');
function showSlide(e) {
    var toShow = e.target.getAttribute('rel');
    for (var i = 0, len = slides.length; i < len; i++) {
        slides[i].style.display = 'none';
    }
    document.getElementById(toShow).style.display = 'block';
    e.preventDefault();
    return false;
}

var links = document.getElementsByClassName('slide-link');

for (var i = 0, len = links.length; i < len; i++) {
    links[i].addEventListener('click', showSlide, false);
}

and example HTML to go with it:

<a href="#" class="slide-link" rel="slide1">Welcome</a>
<a href="#" class="slide-link" rel="slide2">Overview</a>

<div class="slide" id="slide1">
    <p>Welcome etc etc etc</p>
</div>
<div class="slide" id="slide2">
    <p>Overview etc etc etc</p>
</div>

Hopefully that can get you on the right track.

鯉魚旗 2024-10-25 12:05:18

有一个名为 S5 的非常棒的预构建开源实现。看看吧,你可能会非常喜欢它。

There's an awesome, pre-built, open-source implementation called S5. Take a look, you'll probably really like it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文