简体   繁体   English

为什么负保证金不起作用?

[英]Why is negative margin not working?

When you click 'Resume' some text comes in from the left. 当您单击“恢复”时,一些文本从左侧进入。 This is because I gave negative margin to the text initially. 这是因为我最初对文本给出了负余量。

But negative margin of intro_page gives no effect. 但是intro_page负边距没有影响。

What I want is that: 我想要的是:

  1. When I click resume_page , text of resume should come in from left (this works) and text of intro_page should go out to the right (this doesn't work). 当我点击resume_page ,简历的文本应该从左边进入(这个工作), intro_page文本应该向右(这不起作用)。

    Negative margin of intro_page doesn't seem to be working. intro_page负边距似乎不起作用。 Why is that? 这是为什么?

  2. Initially, page should be empty except the two links. 最初,除两个链接外,页面应为空。 Currently, intro_page page is shown. 目前,显示了intro_page页面。 resume_page is hidden properly, as desired. resume_page根据需要正确隐藏。

In short, everything about resume_page works perfectly. 简而言之,关于resume_page一切resume_page完美。 I want the exact same things to happen to intro_page 我想在intro_page发生完全相同的事情

 function clicked_resume(OnScreen) { var resume = document.getElementById('resume_page'); if(OnScreen == 1) { resume.className = ''; clicked_about(0); } else if(OnScreen == 0) { resume.className = 'unseen'; } } function clicked_about(OnScreen) { var about = document.getElementById('intro_page'); if(OnScreen == 1) { about.className = ''; clicked_resume(0); } else if(OnScreen == 0) { about.className = 'unseen'; } } 
 #intro_page.unseen{ display: block; margin-right: -200px; } #intro_page{ margin-right: 0px; display: block; -webkit-transition: margin-right 1.5s ease-in; transition: margin-right 1.5s ease-in; } #resume_page.unseen{ display: block; margin-left: -600px; } #resume_page{ margin-left: 0px; display: block; -webkit-transition: margin-left 1.0s ease-in; transition: margin-left 1.0s ease-in; } 
  <h2 class="title_bar"> <ul> <li><span onclick = "clicked_about(1)">About</span></li> <li><span onclick="clicked_resume(1)">Resume</span></li> </ul> </h2> <div id="intro_page" class="unseen"> <p id="intro_main_text"> I enjoy reading, swimming, jogging, painting and exploring. </p> <figure class="intro_pic1"> <img src="img/award.jpg" alt="Receiving Award" height="50" /> <figcaption>Award 2015</figcaption> </figure> </div> <div id="resume_page" class="unseen"> <p>My resume</p> </div> 

Try using position: relative and right instead of margin-right . 尝试使用position: relativeright而不是margin-right It's more reliable since it doesn't take the element's position into account when positioning other elements, like margin does. 它更可靠,因为它在定位其他元素时不考虑元素的位置,如margin For bonus points you could even use transform: translate instead of right . 对于奖励积分,您甚至可以使用transform: translate而不是right

 function clicked_resume(OnScreen) { var resume = document.getElementById('resume_page'); if(OnScreen == 1) { resume.className = ''; clicked_about(0); } else if(OnScreen == 0) { resume.className = 'unseen'; } } function clicked_about(OnScreen) { var about = document.getElementById('intro_page'); if(OnScreen == 1) { about.className = ''; clicked_resume(0); } else if(OnScreen == 0) { about.className = 'unseen'; } } 
 body { margin: 0; overflow-x: hidden; } #intro_page.unseen{ display: block; right: -100%; } #intro_page{ position: relative; right: 0px; display: block; -webkit-transition: right 1.5s ease-in; transition: right 1.5s ease-in; } #resume_page.unseen{ display: block; left: -600px; } #resume_page{ position: relative; left: 0px; display: block; -webkit-transition: left 1.0s ease-in; transition: left 1.0s ease-in; } 
 <h2 class="title_bar"> <ul> <li><span onclick = "clicked_about(1)">About</span></li> <li><span onclick="clicked_resume(1)">Resume</span></li> </ul> </h2> <div id="intro_page" class="unseen"> <p id="intro_main_text"> I enjoy reading, swimming, jogging, painting and exploring. </p> <figure class="intro_pic1"> <img src="img/award.jpg" alt="Receiving Award" height="50" /> <figcaption>Award 2015</figcaption> </figure> </div> <div id="resume_page" class="unseen"> <p>My resume</p> </div> 

The Reason is that in CSS the elements are laid from left to right, margin-right would never work as you wish. 原因是在CSS中元素是从左到右放置的,margin-right永远不会按你的意愿工作。

Just Modifying your CSS like this, it will work as a charm. 只需像这样修改你的CSS,它就可以作为一个魅力。

 body {
   overflow:hidden
}

#intro_page.unseen{
    transform: translateX(120%);
    transition: transform 1s ease-in;
}
#intro_page{
    transform: translateX(0);
    transition: transform 1s ease-in;
}

#resume_page.unseen{
    transform: translateX(-100%);
    transition: transform 1s ease-in;
}

#resume_page{
    transform: translateX(0);
    transition: transform 1s ease-in;
}

Hint: for better performance in animation always use translations instead of position, margin, width... 提示:为了在动画中获得更好的表现,请始终使用翻译而不是位置,边距,宽度......

http://jsfiddle.net/wgsy6ufx/1/ http://jsfiddle.net/wgsy6ufx/1/

As you can read in this guide , left and right negative margins don't behave the same : negative margin-left pulls the styled element towards the left, while negative margin-right pulls the adjacent right element towards the styled element. 正如您可以在本指南中看到的那样,左右边距的行为不相同:负margin-left样式元素拉向左侧,而负margin-right相邻的右侧元素拉向样式元素。 It doesn't move the styled element itself. 它不会移动样式元素本身。

If you want to achieve your output, use position: relative and the left property to position your intro div in the far right : see this fiddle . 如果你想获得你的输出,使用position: relativeleft属性来将你的intro div放在最右边:看看这个小提琴

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

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