如何使用 JavaScript 创建水平和垂直标签?

选项卡可用于以有组织的方式在单个页面上显示大量内容。我们可以使用 HTML、CSS 和 JavaScript 设计单页选项卡。HTML 元素用于设计选项卡的结构及其段落中的内容。样式是使用 CSS 执行的。单击每个选项卡按钮时,选项卡将显示其各自的内容。这是使用 JavaScript 完成的。

水平制表符:以下代码演示了带有制表符的简单 HTML 结构及其以段落形式的内容。单击每个选项卡时,它会调用下面给出的“script.js”文件中实现的displayContent()方法。

HTML 代码: 

  • HTML

<!DOCTYPE html>
<html>
  
<head>
    <link rel="stylesheet" href="style.css">
    <script src="script.js"></script>
</head>
  
<body>
    <h2 style="color:green">
        GeeksforGeeks Horizontal tabs
    </h2>
  
    <!-- Link to each tab with onclick event -->
    <div id="tabsDiv">
        <button class="linkClass" onclick=
            "displayContent(event, 'interview')">
            Interview
        </button>
  
        <button class="linkClass" onclick=
            "displayContent(event, 'practice')">
            Practice
        </button>
  
        <button class="linkClass" onclick=
            "displayContent(event, 'python')">
            Python
        </button>
  
        <button class="linkClass" onclick=
            "displayContent(event, 'algorithms')">
            Algorithms
        </button>
  
        <button class="linkClass" onclick=
            "displayContent(event, 'machine')">
            Machine Learning
        </button>
    </div>
  
    <!-- Content for each HTML tab  -->
    <div id="interview" class="contentClass">
        <h3>Interview</h3>
  
        <p>
            Also, in Asymptotic analysis, we always
            talk about input sizes larger than a
            constant value. It might be possible
            that those large inputs are never given
            to your software and an algorithm which is
            asymptotically slower, always performs
            better for your particular situation.
            So, you may end up choosing an algorithm
            that is Asymptotically slower but faster
            for your software.
        </p>
  
    </div>
  
    <div id="practice" class="contentClass">
        <h3>Practice</h3>
  
        <p>
            Asymptotic Analysis is the big idea
            that handles above issues in analyzing
            algorithms. In Asymptotic Analysis,
            we evaluate the performance of an 
            algorithm in terms of input size (we 
            don’t measure the actual running time).
            We calculate, how does the time
            (or space) taken by an algorithm
            increases with the input size.
        </p>
  
    </div>
  
    <div id="python" class="contentClass">
        <h3>Python</h3>
  
        <p>
            Python is a high-level, general-purpose
            and a very popular programming language.
            Python programming language (latest Python 3)
            is being used in web development, Machine
            Learning applications, along with all
            cutting edge technology in Software Industry.
            Python Programming Language is very well
            suited for Beginners, also for experienced
            programmers with other programming languages
            like C++ and Java.
        </p>
    </div>
  
    <div id="algorithms" class="contentClass">
        <h3>Greedy Algorithms</h3>
  
        <p>
            Greedy is an algorithmic paradigm that
            builds up a solution piece by piece,
            always choosing the next piece that
            offers the most obvious and immediate
            benefit. So the problems where
            choosing locally optimal also
            leads to global solution are
            best fit for Greedy.
        </p>
    </div>
  
    <div id="machine" class="contentClass">
        <h3>Machine Learning</h3>
  
        <p>
            Machine Learning is the field of
            study that gives computers the capability
            to learn without being explicitly
            programmed. ML is one of the most
            exciting technologies that one
            would have ever come across.
            As it is evident from the name,
            it gives the computer that makes
            it more similar to humans:
            The ability to learn. Machine learning
            is actively being used today,
            perhaps in many more places than
            one would expect.
        </p>
    </div>
</body>
  
</html>

CSS 代码:以下代码属于“style.css”文件,用于设置页面和选项卡的样式。

  • css

<style>
    h3 {
        text-align: left;
    }
  
    /* Button to open the content */
    .linkclass {
        float: left;
        cursor: pointer;
        padding: 10px 15px 10px 10px;
        background-color: light-grey;
    }
  
    /* Button styling on mouse hover */
    #tabsDiv a:hover {
        color: black;
        background-color: #e9e9e9;
        font-size: 16px;
    }
  
    /* Change the color of the button */
    button.active {
        background-color: #c0c0c0;
    }
  
    /* Content for button tabs*/
    .contentClass {
        display: none;
        padding: 10px 16px;
        border: 2px solid #c0c0c0;
    }
</style>

JavaScript 代码:以下代码包含使用 JavaScript 的选项卡的功能。

  • Javascript

function displayContent(event, contentNameID) {
  
    let content =
        document.getElementsByClassName("contentClass");
    let totalCount = content.length;
  
    // Loop through the content class
    // and hide the tabs first
    for (let count = 0;
        count < totalCount; count++) {
        content[count].style.display = "none";
    }
  
    let links =
        document.getElementsByClassName("linkClass");
    totalLinks = links.length;
  
    // Loop through the links and
    // remove the active class
    for (let count = 0;
        count < totalLinks; count++) {
        links[count].classList.remove("active");
    }
  
    // Display the current tab
    document.getElementById(contentNameID)
        .style.display = "block";
  
    // Add the active class to the current tab
    event.currentTarget.classList.add("active");
}

输出: 
 

垂直选项卡:可以通过更改 HTML 结构并将 CSS 样式表替换为用于垂直选项卡设计的修改后的样式表来使选项卡垂直。以下代码演示了垂直选项卡。

HTML 代码: 

  • HTML

<!DOCTYPE html>
<html>
  
<head>
    <link rel="stylesheet" href="vstyle.css">
    <script src="script.js"></script>
</head>
  
<body>
    <h2 style="color:green">
        GeeksforGeeks Vertical tabs
    </h2>
  
    <div id="tabsDiv">
        <div id="interview" class="contentClass">
            <h3>Interview</h3>
  
            <p>
                Also, in Asymptotic analysis,
                we always talk about input sizes larger
                than a constant value. It might
                be possible that those large inputs
                are never given to your software and
                an algorithm which is asymptotically
                slower, always performs better for
                your particular situation. So, you
                may end up choosing an algorithm that
                is Asymptotically slower but faster
                for your software.
            </p>
        </div>
  
        <div id="practice" class="contentClass">
            <h3>Practice</h3>
  
            <p>
                Asymptotic Analysis is the big idea
                that handles above issues in analyzing
                algorithms. In Asymptotic Analysis, we
                evaluate the performance of an algorithm
                in terms of input size (we don’t measure
                the actual running time). We calculate,
                how does the time (or space) taken by
                an algorithm increases with
                the input size.
            </p>
        </div>
  
        <div id="python" class="contentClass">
            <h3>Python</h3>
  
            <p>
                Python is a high-level, general-purpose
                and a very popular programming language.
                Python programming language (latest Python 3)
                is being used in web development, Machine
                Learning applications, along with all
                cutting edge technology in Software Industry.
                Python Programming Language is very
                well suited for Beginners, also for
                experienced programmers with other
                programming languages like C++ and Java.
            </p>
        </div>
  
        <div id="algorithms" class="contentClass">
            <h3>Greedy Algorithms</h3>
  
            <p>
                Greedy is an algorithmic paradigm
                that builds up a solution piece by
                piece,always choosing the next piece
                that offers the most obvious and
                immediate benefit. So the problems
                where choosing locally optimal also
                leads to global solution are best
                fit for Greedy.
            </p>
        </div>
  
        <ul class="ulClass" style="height:300px">
            <li style="list-style-type:none;">
                <button class="linkClass" onclick=
                    "displayContent(event, 'interview')">
                    Interview
                </button>
            </li>
              
            <li style="list-style-type:none;">
                <button class="linkClass" onclick=
                    "displayContent(event, 'practice')">
                    Practice
                </button>
            </li>
  
            <li style="list-style-type:none;">
                <button class="linkClass" onclick=
                    "displayContent(event, 'python')">
                    Python
                </button>
            </li>
  
            <li style="list-style-type:none;">
                <button class="linkClass" onclick=
                    "displayContent(event, 'algorithms')">
                    Algorithms
                </button>
            </li>
        </ul>
    </div>
</body>
  
</html>

CSS 代码:下面的代码是修改后的 CSS 代码,用于垂直标签显示。

  • css

<style>
    * {
        box-sizing: border-box;
    }
  
    #tabsDiv {
        height: 300px;
        border: 2px solid #c0c0c0;
    }
  
    #tabsDiv ul {
        height: 300px;
        padding: 0px 5px;
    }
  
    #tabsDiv li {
        width: 15%;
        height: 60px;
    }
  
    #tabsDiv button {
        float: left;
        border: 1px solid #c0c0c0;
        background-color: #f1f0f4;
    }
  
    /* Button to open the content */
    #tabsDiv button {
        display: block;
        background-color: inherit;
        color: black;
        padding: 25px 15px;
        width: 100%;
        text-align: left;
        cursor: pointer;
    }
  
    /* Button styling on mouse hover */
    #tabsDiv button:hover {
        background-color: #d1d1d1;
        color: lime;
    }
  
    #tabsDiv button.active {
        background-color: #c0c0c0;
    }
  
    /* Content for tabs*/
    .contentClass {
        display: none;
        position: absolute;
        left: 18%;
        padding: 0px 15px;
        width: 70%;
        border-style: none;
        height: 300px;
    }
</style>

输出:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 移动端块级标签左右滑动可以通过以下步骤实现: 1. 给外层容器设置固定宽度和 `overflow-x: scroll`,使其可以水平滚动。 2. 内部的块级标签可以使用 `display: inline-block` 或 `float: left` 等样式让它们排列在一行。 3. 如果需要添加滑动效果,可以使用 JavaScript 监听 touch 事件,根据手指移动的距离调整容器的 `scrollLeft` 值,实现左右滑动的效果。 以下是一个示例代码: HTML: ``` <div class="wrapper"> <div class="block">Block 1</div> <div class="block">Block 2</div> <div class="block">Block 3</div> <div class="block">Block 4</div> <div class="block">Block 5</div> </div> ``` CSS: ``` .wrapper { width: 100%; overflow-x: scroll; white-space: nowrap; /* 让内部块级标签在一行中排列 */ } .block { display: inline-block; width: 100px; height: 100px; background-color: #ccc; } ``` JavaScript: ``` var wrapper = document.querySelector('.wrapper'); var startX, startY, moveX, moveY, distanceX, distanceY; wrapper.addEventListener('touchstart', function(e) { startX = e.touches[0].pageX; startY = e.touches[0].pageY; }); wrapper.addEventListener('touchmove', function(e) { moveX = e.touches[0].pageX; moveY = e.touches[0].pageY; distanceX = moveX - startX; distanceY = moveY - startY; if (Math.abs(distanceX) > Math.abs(distanceY)) { // 判断是水平滑动还是垂直滑动 e.preventDefault(); // 阻止默认事件,使页面不会滚动 wrapper.scrollLeft -= distanceX; // 调整容器的 scrollLeft 值 } }); ``` ### 回答2: 移动端块级标签左右滑动是指在移动设备上,通过手指滑动屏幕实现对块级标签的左右滑动操作。 实现移动端块级标签左右滑动有多种方式,以下是其中一种常见的实现方法: 首先,需要使用HTML和CSS创建一个具有滚动效果的容器,容器内放置需要左右滑动的块级标签。 其次,使用JavaScript监听触摸事件,并根据手指滑动的方向和距离,改变容器内块级标签的位置。 具体实现步骤如下: 1. HTML布局部分,创建一个div容器,设置样式为可滚动,并放置需要左右滑动的块级标签。例如: <div class="scroll-container"> <div class="scroll-content"> <!-- 块级标签内容 --> </div> </div> 2. CSS样式部分,对容器和块级标签进行样式设置,其中.scroll-container需要设置overflow属性为scroll或auto以实现滚动效果。 .scroll-container { overflow: scroll; } .scroll-content { /* 块级标签样式设置 */ } 3. JavaScript部分,监听触摸事件,并根据手指滑动的方向和距离,改变.scroll-container容器的滚动位置。例如: var container = document.querySelector('.scroll-container'); var startX; // 初始触摸位置 container.addEventListener('touchstart', function(e) { startX = e.touches[0].clientX; // 记录初始触摸位置 }); container.addEventListener('touchmove', function(e) { var moveX = e.touches[0].clientX; // 移动时的触摸位置 var distanceX = moveX - startX; // 滑动距离 // 根据滑动距离改变.scroll-container容器的滚动位置 container.scrollLeft -= distanceX; e.preventDefault(); // 阻止默认事件,避免页面滚动 }); 通过以上步骤实现了移动端块级标签的左右滑动效果。可以根据实际需求进行样式和交互的调整。 ### 回答3: 移动端块级标签左右滑动是指在移动设备上,通过手指滑动屏幕实现块级标签的左右切换或滚动效果。 要实现移动端块级标签左右滑动,可以使用一些现有的前端框架或库,如Swiper、Slick等。这些框架提供了丰富的API和功能,可以方便地实现移动端块级标签的左右滑动效果。 首先,需要将块级标签按照一定的布局方式排列,并提供导航按钮或指示器来切换不同的块级标签。在HTML文件中,可以使用div元素来包裹每个块级标签,并设置合适的CSS样式,使它们水平排列。 然后,使用JavaScript来实现滑动效果。通过绑定滑动事件,监听用户手指在屏幕上的滑动方向和距离,根据滑动的方向来切换不同的块级标签。可以使用一些原生的JavaScript方法,如touchstart、touchmove、touchend等来实现手势的监听和处理。 当用户滑动屏幕时,可以通过计算手指滑动的距离和滑动的方向,来判断是否切换到下一个或上一个块级标签。在切换过程中,可以使用CSS3动画或过渡效果,来实现流畅的滑动过程。 此外,为了提高用户体验,可以为移动端块级标签左右滑动添加一些特效和动画效果。比如,在切换到下一个块级标签时,可以使用渐变效果或滑动动画,使切换过程更加平滑和自然。 总之,移动端块级标签的左右滑动可以通过使用前端框架或库以及一些JavaScript和CSS技术来实现,提供给用户更加流畅和友好的交互体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值