简体   繁体   English

使用javascript css隐藏show div

[英]hide show div using javascript css

I want to slowly hide or show my div . 我想慢慢隐藏或展示我的div。 what should be the logic in script bellow .When i clicked the button,existing div hiding and another div showing. 下面的脚本应该是什么逻辑。当我点击按钮时,现有的div隐藏和另一个div显示。 would anyone please answer.Here is my code snippet. 请问有人请回答。这是我的代码片段。

  function myFunction() { var x = document.getElementById('before-e'); var y = document.getElementById('after-e'); if (x.style.display === 'none') { x.style.display = 'block'; } else { y.style.display = 'block'; x.style.display = 'none'; } } 
 .favorites-right-before-expand { background-color: #5642BE; color: #fff; height: 155px; width: 106px; margin-left: -8px; } .matched-body { padding-top: 40px; } .per { margin-bottom: -8px; } .prog-percent { margin-left: 9px; font-size: 16px; } .prog-percent { margin-left: 9px; font-size: 16px; } .my-progress { height: 10px; margin-bottom: 0px; border-radius: 0px; margin-left: 9px; margin-right: 15px; } .favorites-right-before-expand button { color: #D6DA22; font-size: 8px; text-decoration: underline; margin-left: 2px; background: none; border-style: none; } .per { margin-bottom: -8px; } .prog-percent-after { margin-left: 21px; font-size: 16px; } .prog-percent-after { margin-left: 21px; font-size: 16px; } .my-progress-after { height: 10px; margin-bottom: 0px; border-radius: 0px; margin-left: 21px; margin-right: 42px; } .expand ul { list-style: none; color: #D6DA22; font-size: 10px; margin-left: -17px; position: relative; margin-top: 7px; } .favorites-right { background-color: #5642BE; color: #fff; height: 180px; width: 142px; padding-top: 13px; } 
 <div class="favorites-right" style="display: none;" id="after-e"> <div class="prog-percent-after per">80%</div> <div class="prog-percent-after"> MATCHED</div> <div class="progress my-progress-after"> <div class="progress-bar" role="progressbar" aria-valuenow="80" aria-valuemin="0" aria-valuemax="100" style="width:80%"> </div> </div> <div class="expand"> <ul> <li> You both do not smoke</li> <li> You both live in Bangldesh</li> <li> Ridhika seems to like photography</li> <a href="#" style="color: #D6DA22;font-size: 10px;text-decoration: underline;">See more>></a> </ul> </div> </div><!-- end favorites-right--> <div class="favorites-right-before-expand" id="before-e"> <div class="matched-body"> <div class="prog-percent per">80%</div> <div class="prog-percent"> MATCHED</div> <div class="progress my-progress"> <div class="progress-bar" role="progressbar" aria-valuenow="80" aria-valuemin="0" aria-valuemax="100" style="width:80%"> </div> </div> <button onclick="myFunction()" class="expand_button">Expand Categories>></button> </div><!--end matched-body--> </div><!-- end favorites-right-before-expand --> 

I think that you are making this harder on yourself than you need to be. 我认为你在自己身上做的比你需要的更难。 Assuming that you are using jQuery you can easily just do the following. 假设您正在使用jQuery,您可以轻松地执行以下操作。

function toggleDivs () {
  $('#after-e').toggle();
  $('#before-e').toggle();
}

The toggle function will display or hide the elements based on their current state in the dom. 切换功能将根据dom中的当前状态显示或隐藏元素。

If you want to fade one element out, then fade in another one after the animation has completed, you can pass in a function into the second parameter of the .fadeOut function. 如果要淡出一个元素,然后在动画完成后淡入另一个元素,则可以将函数传入.fadeOut函数的第二个参数。 In this function, you .fadeIn the second element. 在这个函数中,你.fadeIn第二个元素。

Here's an Example 这是一个例子

500 is the delay in milliseconds 500是以毫秒为单位的延迟

$('#first-element').fadeOut(500, function() {
    $('#second-element').fadeIn();
});

The example in your code 代码中的示例

 function myFunction() { $('#before-e').fadeOut(500, function() { $('#after-e').fadeIn(); }); } 
 .favorites-right-before-expand { background-color: #5642BE; color: #fff; height: 155px; width: 106px; margin-left: -8px; } .matched-body { padding-top: 40px; } .per { margin-bottom: -8px; } .prog-percent { margin-left: 9px; font-size: 16px; } .prog-percent { margin-left: 9px; font-size: 16px; } .my-progress { height: 10px; margin-bottom: 0px; border-radius: 0px; margin-left: 9px; margin-right: 15px; } .favorites-right-before-expand button { color: #D6DA22; font-size: 8px; text-decoration: underline; margin-left: 2px; background: none; border-style: none; } .per { margin-bottom: -8px; } .prog-percent-after { margin-left: 21px; font-size: 16px; } .prog-percent-after { margin-left: 21px; font-size: 16px; } .my-progress-after { height: 10px; margin-bottom: 0px; border-radius: 0px; margin-left: 21px; margin-right: 42px; } .expand ul { list-style: none; color: #D6DA22; font-size: 10px; margin-left: -17px; position: relative; margin-top: 7px; } .favorites-right { background-color: #5642BE; color: #fff; height: 180px; width: 142px; padding-top: 13px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="favorites-right" style="display: none;" id="after-e"> <div class="prog-percent-after per">80%</div> <div class="prog-percent-after"> MATCHED</div> <div class="progress my-progress-after"> <div class="progress-bar" role="progressbar" aria-valuenow="80" aria-valuemin="0" aria-valuemax="100" style="width:80%"> </div> </div> <div class="expand"> <ul> <li> You both do not smoke</li> <li> You both live in Bangldesh</li> <li> Ridhika seems to like photography</li> <a href="#" style="color: #D6DA22;font-size: 10px;text-decoration: underline;">See more>></a> </ul> </div> </div><!-- end favorites-right--> <div class="favorites-right-before-expand" id="before-e"> <div class="matched-body"> <div class="prog-percent per">80%</div> <div class="prog-percent"> MATCHED</div> <div class="progress my-progress"> <div class="progress-bar" role="progressbar" aria-valuenow="80" aria-valuemin="0" aria-valuemax="100" style="width:80%"> </div> </div> <button onclick="myFunction()" class="expand_button">Expand Categories>></button> </div><!--end matched-body--> </div><!-- end favorites-right-before-expand --> 

You can add these lines to your code in order to get it through CSS. 您可以将这些行添加到代码中,以便通过CSS获取它。

.favorites-right-before-expand {
    background-color: #5642BE;
    color: #fff;
    height: 155px;
    width: 106px;
    margin-left: -8px;
    opacity: 0;
    transition: visibility 0s, opacity 0.5s linear;
}

Try adding 尝试添加

(yourvariable).style.display = "block";

then add the css: 然后添加css:

@-webkit-keyframes fadein {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}
@keyframes fadein {
    from {
        opacity: 0;
    }
    to {
         opacity: 1;
    }
}

then, 然后,

.yourdiv {
-webkit-animation: fadein 0.5s;
animation: fadein 0.5s; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM