【问题标题】:Convert Jquery slidetoggle code to Javascript将 Jquery slidetoggle 代码转换为 Javascript
【发布时间】:2015-04-29 16:21:06
【问题描述】:

如何将 jQuery slidetoggle() 函数转换为 Javascript?

 var $context = getContext(context);

        $($context).on('click', '.m_menu', function () {
            $('.w_nav').slideToggle();
        });

【问题讨论】:

标签: javascript jquery


【解决方案1】:

这不是将 jQuery 代码转换为 javascript,而是在纯 javascript 中进行。它可以通过不同的方式来实现。我想到了两个:

HTML:

<button id="mbtn" onclick="slideToggle()">SlideToggle</button>
<div id="mdiv">Some context</div>

1.使用 javascript 的 setInterval :
有一个布尔值来跟踪我们是否需要向上滑动或向下滑动(切换)并使用setInterval 来增加/减少高度。

jsfiddle DEMO

Javascript:

var slideOpen = true;
var heightChecked = false;
var initHeight = 0;
var intval = null;

function slideToggle() {
    window.clearInterval(intval);
    var mdiv = document.getElementById('mdiv');
    if(!heightChecked) {
        initHeight = mdiv.offsetHeight;
        heightChecked = true;
    }
    if(slideOpen) {
        var h = initHeight;
        slideOpen = false;
        intval = setInterval(function(){
            h--;
            mdiv.style.height = h + 'px';
            if(h <= 0)
                window.clearInterval(intval);
            }, 1
        );
    }
    else {
        var h = 0;
        slideOpen = true;
        intval = setInterval(function(){
            h++;
            mdiv.style.height = h + 'px';
            if(h >= initHeight)
                window.clearInterval(intval);
            }, 1
        );
    }
}

2。使用CSS3 transition:
从 CSS3 过渡中获得帮助以配合 javascript,这将更容易实现幻灯片效果。然后我们只需要在javascript中改变高度,剩下的就完成了。

jsfiddle DEMO

CSS:

#mdiv {
    /* other styles */
    -webkit-transition: all 1s ease-in-out;
    transition: all 1s ease-in-out;
}

Javascript:

var slideOpen = true;
var heightChecked = false;
var initHeight = 0;

function slideToggle() {
    var mdiv = document.getElementById('mdiv');
    if(!heightChecked) {
        initHeight = mdiv.offsetHeight;
        heightChecked = true;
    }
    if(slideOpen) {
        slideOpen = false;
        mdiv.style.height = '0px';
    }
    else {
        slideOpen = true;
        mdiv.style.height = initHeight + 'px';
    }
}

编辑:
如果我们希望起始高度为 0,那么我们需要进行一些更改:

var slideOpen = false;
//var heightChecked = false;
var initHeight = 120; //height of the element when it's fully open

我们需要注释掉这一点:

/*
if(!heightChecked) {
    initHeight = mdiv.offsetHeight;
    heightChecked = true;
}
*/

jsfiddle DEMO

编辑 #2
正如 Sandro 指出的那样,上下文实际上并没有被隐藏,因此更新了小提琴并添加了 overflow-y: hidden; 并更改了文本颜色以提高可见性。 也将 open 更改为 slideOpen,因为 open 是一个保留字。

【讨论】:

  • 这正是我想要完成的。谢谢!
  • 起始高度为0时如何实现,点击按钮时增加?
  • 无法检查 arm,但您可能需要 2 处更改。首先将 open 的初始值更改为 false 并给 initHeigh 实际高度并删除第一个 initHeight。当我在电脑后面时,我会在几个小时后检查。
  • 内容some context在盒子滑动关闭后并没有真正隐藏。它的颜色与背景颜色匹配,但它仍然存在。如果应该关闭的框的文本与周围的背景颜色相同,我猜这应该不起作用。但 chebaby 的回答确实如此。
  • @Sandro 你说得对,将overflow-y: hidden;(或溢出:隐藏;)添加到滑动元素将解决问题。更新了答案和小提琴。谢谢。
【解决方案2】:

要将 jquery slideToggle() 重新实现(转换)为 vanilla javascript,您需要将 jquery slideUp() 和 slideDown() 重新实现为好吧。

var DOMAnimations = {
    
    /**
    * SlideUp
    *
    * @param {HTMLElement} element
    * @param {Number} duration
    * @returns {Promise<boolean>}
    */
    slideUp: function (element, duration = 500) {

        return new Promise(function (resolve, reject) {

            element.style.height = element.offsetHeight + 'px';
            element.style.transitionProperty = `height, margin, padding`;
            element.style.transitionDuration = duration + 'ms';
            element.offsetHeight;
            element.style.overflow = 'hidden';
            element.style.height = 0;
            element.style.paddingTop = 0;
            element.style.paddingBottom = 0;
            element.style.marginTop = 0;
            element.style.marginBottom = 0;
            window.setTimeout(function () {
                element.style.display = 'none';
                element.style.removeProperty('height');
                element.style.removeProperty('padding-top');
                element.style.removeProperty('padding-bottom');
                element.style.removeProperty('margin-top');
                element.style.removeProperty('margin-bottom');
                element.style.removeProperty('overflow');
                element.style.removeProperty('transition-duration');
                element.style.removeProperty('transition-property');
                resolve(false);
            }, duration)
        })
    },

    /**
    * SlideDown
    *
    * @param {HTMLElement} element
    * @param {Number} duration
    * @returns {Promise<boolean>}
    */
    slideDown: function (element, duration = 500) {

        return new Promise(function (resolve, reject) {

            element.style.removeProperty('display');
            let display = window.getComputedStyle(element).display;

            if (display === 'none') 
                display = 'block';

            element.style.display = display;
            let height = element.offsetHeight;
            element.style.overflow = 'hidden';
            element.style.height = 0;
            element.style.paddingTop = 0;
            element.style.paddingBottom = 0;
            element.style.marginTop = 0;
            element.style.marginBottom = 0;
            element.offsetHeight;
            element.style.transitionProperty = `height, margin, padding`;
            element.style.transitionDuration = duration + 'ms';
            element.style.height = height + 'px';
            element.style.removeProperty('padding-top');
            element.style.removeProperty('padding-bottom');
            element.style.removeProperty('margin-top');
            element.style.removeProperty('margin-bottom');
            window.setTimeout(function () {
                element.style.removeProperty('height');
                element.style.removeProperty('overflow');
                element.style.removeProperty('transition-duration');
                element.style.removeProperty('transition-property');
            }, duration)
        })
    },

    /**
    * SlideToggle
    *
    * @param {HTMLElement} element
    * @param {Number} duration
    * @returns {Promise<boolean>}
    */
    slideToggle: function (element, duration = 500) {

        if (window.getComputedStyle(element).display === 'none') {

            return this.slideDown(element, duration);

        } else {

            return this.slideUp(element, duration);
        }
    }
}

// ------------------------------------------------------

document.addEventListener("DOMContentLoaded", function() {

    var button = document.getElementById('slideToggle');

    var cardElement = document.getElementById('firstCard');

    button.addEventListener('click', function(event) {

        event.preventDefault();

        DOMAnimations.slideToggle(cardElement);
    });

});
* {
    box-sizing: border-box;
}

/* Add a gray background color with some padding */
body {
    font-family: Arial;
    padding: 20px;
    background: #f1f1f1;
}

/* Header/Blog Title */
.header {
    padding: 10px;
    font-size: 24px;
    text-align: center;
    background: white;
}

/* Create two unequal columns that floats next to each other */
/* Left column */
.leftcolumn {   
    float: left;
    width: 100%;
}

/* Fake image */
.fakeimg {
    background-color: #aaa;
    width: 100%;
    padding: 20px;
}

/* Add a card effect for articles */
.card {
    position:relative;
    background-color: white;
    padding: 20px;
    margin-top: 20px;
}

#slideToggle {
    background-color: #f9f5f5;
    color: black;
    border: 2px solid #a9b5a9;
    padding: 5px;
    margin-top:20px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 16px;
    cursor: pointer;
    font-weight: bold;
    border-radius: 5px;
}

/* Clear floats after the columns */
.row:after {
    content: "";
    display: table;
    clear: both;
}

/* Footer */
.footer {
    padding: 20px;
    text-align: center;
    background: #ddd;
    margin-top: 20px;
}

/* Responsive layout - when the screen is less than 800px wide, make the two columns stack on top of each other instead of next to each other */
@media screen and (max-width: 800px) {
    .leftcolumn, .rightcolumn {   
        width: 100%;
        padding: 0;
    }
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>

<div class="header">
    <h2>Blog Name</h2>
</div>

<div class="row">
    <div class="leftcolumn">
        <button id="slideToggle">slideToggle</button>
        <div class="card" id="firstCard">
            <h2>FIRST TITLE HEADING</h2>
            <h5>Title description, Dec 7, 2018</h5>
            <div class="fakeimg" style="height:200px;">Image</div>
            <p>Some text..</p>
            <p>Sunt in culpa qui officia deserunt mollit anim id est laborum consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco.</p>
        </div>
        <div class="card">
            <h2>SECOND TITLE HEADING</h2>
            <h5>Title description, Dec 7, 2018</h5>
            <div class="fakeimg" style="height:200px;">Image</div>
            <p>Some text..</p>
            <p>Sunt in culpa qui officia deserunt mollit anim id est laborum consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco.</p>
        </div>
    </div>
</div>

<div class="footer">
    <h2>Footer</h2>
</div>

</body>
</html>

【讨论】:

  • 我认为这个答案“更正确”,因为 OP 声明“将 Jquery 幻灯片切换代码转换为 Javascript”。很抱歉参加聚会有点晚了......
【解决方案3】:

我给出了一个轻量级的答案。您可以将其视为一个骨架示例,您可以根据自己的喜好使用它,稍后设置样式。

在开始之前,我想澄清一下,我不会将 jQuery slideToggle() 函数转换为 JavaScript 中的一个函数

我所做的只是尝试以最简单的形式复制该功能!

第 1 步:

创建一个按钮/(任何其他元素),您可以将其与eventListener 链接。

第 2 步:

创建一个你想要切换/滑动/任何你想调用它的元素!

第 3 步:

继续使用您的javaScript,并为与该eventListener 链接的函数创建一个条件 setInterval()。

您可以在评论部分提出您的建议。

var animxB = 0;
var anim2B = 0;
document.getElementById("animB").addEventListener("click", performB);

function performB() {
anim2B++;
anim3B = anim2B % 2;
var fram1B = setInterval(framB, 1);

function framB() {
    if (anim3B == 1) {
        animxB++;
        document.getElementById("divB").style.width = animxB + "px";
        document.getElementById("divB").style.height = animxB + "px";
        if (animxB >= 200) {
            clearInterval(fram1B);
            document.getElementById("animB").innerHTML = "Click(-)";
        }
    }
    if (anim3B == 0) {
        animxB--;
        document.getElementById("divB").style.width = animxB + "px";
        document.getElementById("divB").style.height = animxB + "px";
        if (animxB <= 0) {
            clearInterval(fram1B);
            document.getElementById("animB").innerHTML = "Click(+)";
        }
    }


}
}
<!DOCTYPE html>
<html>

<head></head>

<body>
<button id="animB" style="font-weight:bold; color:brown;">Click(+)</button>
<div id="divB" style="width:0; height:0; overflow:auto; color:gray; font-family:sans-serif; font-size:13px;">
    &lt;path d="M10 10 v80 h80 v-80 h-80" fill="gray" stroke="black" stroke-width="1" /&gt; &lt;path d="M40 10 v20" stroke="blue" stroke-width="0.5" /&gt; &lt;path d="M10 30 h30" stroke="red" stroke-width="0.5" /&gt; &lt;circle cx="40" cy="30" r="2" fill="purple" stroke="purple" stroke-width="1" /&gt;
</div>
</body>

</html>

【讨论】:

    【解决方案4】:

    香草更简单的解决方案我把它留在这里 html->

    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <link rel="stylesheet" href="css.css" />
        <script src="js.js"></script>
        <title>Document</title>
      </head>
      <body>
        <div class="menu">
          <div class="dropdown">Menu</div>
          <div class="menu_main">
            <div class="menu__item">Item 1</div>
            <div class="menu__item">Item 2</div>
            <div class="menu__item">Item 3</div>
          </div>
        </div>
        <div>
          text behind of menu
        </div>
      </body>
    </html>
    

    CSS

    * {
      box-sizing: border-box;
    }
    .menu {
      display: block;
      position: relative;
      background-color: rgba(255, 0, 0, 0.3);
      width: 150px;
      height: 40px;
      font-family: monospace;
    }
    
    .dropdown {
      background-color: #4caf50;
      box-shadow: inset 2px 2px 30px 10px lime;
      display: block;
      cursor: pointer;
      text-align: center;
      padding: 7px 35px;
      color: white;
      position: relative;
      width: 100%;
      height: 100%;
      font-size: 1.5em;
      font-weight: 900;
      box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
    }
    .menu_main {
      position: absolute;
      height: 0%; /* 300 */
      width: 100%;
      overflow: hidden;
      transition: height 1250ms;
      background-color: rgba(0, 255, 0, 0.2);
    }
    
    .menu__item {
      font-size: 1.4em;
      text-align: center;
      font-weight: 900;
      line-height: 40px;
      height: 34%;
      background-color: rgb(189, 243, 201);
    }
    

    JS

    document.addEventListener("DOMContentLoaded", function () {
      const boton = document.getElementsByClassName("dropdown")[0];
    
      boton.addEventListener(
        "click",
        function (e) {
          if (
            window
              .getComputedStyle(e.target.nextElementSibling, null)
              .getPropertyValue("height") == "0px"
          ) {
            e.target.nextElementSibling.style.height = "300%";
          } else {
            e.target.nextElementSibling.style.height = "0%";
          }
        },
        false
      );
    });
    

    此代码没有 slideUp 和 SlideDown,它有所有在一起。使用一次

    【讨论】:

    • 能否添加demo
    【解决方案5】:
    You Just need Vanilla Js to accomplish what you need, by the way Here is the laziest way
    
    //i  hope this will solve your issue
    <html lang="en-US">
      <head>
       <meta charset="utf-8">
        <meta name="viewport" content="width=device-width,initial-scale=0.1,shrink-to-fit=no">
         <link type="text/css" href="/css" rel="stylesheet">
          <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
           <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
           <title>slideToggle</title>
            <style>
    .toggle-navigation{
      transition: all .3s ease-in-out !important;
      margin:100px auto;
    }
    .toggle-content{
    transition: all .3s ease-in-out !important;
    }
     </style>
    </head>
       <body>
        <div class="toggle navigation bg-primary w-25" id="toggle-nav" onclick = "toggleNavigation();">
         <p class="text-white text-center font-weight-bold">Click To Expand</p>
          <div class="toggle-content" id="toggle-content">
           <p class="text-white font-weight-bold text-justify">
    Lorem Ipsum Dolor Sit Amet , Lorem Ipsum Dolor Sit Amet
    Lorem Ipsum Dolor Sit Amet , Lorem Ipsum Dolor Sit Amet
    Lorem Ipsum Dolor Sit Amet , Lorem Ipsum Dolor Sit Amet
    Lorem Ipsum Dolor Sit Amet , Lorem Ipsum Dolor Sit Amet
    Lorem Ipsum Dolor Sit Amet , Lorem Ipsum Dolor Sit Amet
             </p>
            </div>
           </div>
    <script>
    let toggleNav = document.getElementById('toggle-nav');
        navHeight = toggleNav.offsetHeight;
        content= document.getElementById('toggle-content');
        contentHeight = content.offsetTop - content.offsetTop;
        content.style.height = contentHeight;
    
        toggle = true ;
        function toggleNavigation(){
        if(toggle){
        content.style.height = navHeight;
        }else{
        content.style.height = contentHeight;
        }
        toggle = !toggle;
    }
        </script>
       </body>
      </html>
    
     
    

    【讨论】:

      猜你喜欢
      • 2012-03-20
      • 1970-01-01
      • 2022-12-02
      • 2012-03-08
      • 1970-01-01
      • 1970-01-01
      • 2020-07-07
      • 2021-09-25
      • 2012-03-04
      相关资源
      最近更新 更多