How do I stop the page from expanding when animating an element so it slides in from off screen?

白昼怎懂夜的黑 提交于 2021-01-29 09:27:51

问题


I'm making my first website and I've run into a problem I can't easily fix as I'm not sure how to phrase a google search for it. I need to slide images into the page without making the scroll bar appear or, rather, without the page expanding in width to encompass the newly appeared image while it slides in.

Here's the actual test version of the page:

http://test.dingac.com/accommodation.html

The part I need help with is the sliding of the pictures when you click on the arrows next to the blueprint for each apartment in the accommodation tab. If you want to look at the code, the relevant code is in the JqueryAnimate.js file but keep in mind, the comments aren't in English, I'm new to this so the code is a bit weird and the CSS isn't fine tuned yet. I've posted the relevant code snippet further down. My current issue is the slide animation. The way I did it right now is for all the images to be there from the start but all but one have display:none. When you click the arrow to the right it fades out and slides out the current picture (using Jquery) and turns on the display of the next picture (which is positioned relatively at left: 2000px) while animating it to left:0px. In the moment the new image appears, the page sees that a new element is on the page and not everything is being displayed so it expands the width of the page to encompass the off-screen picture. This is only an annoyance on desktop as it only makes the scroll bar appear, but on mobile it makes the whole page zoom out until the new picture is on screen and then zoom back in as the picture slides in.

$("#buttonRight"+apInd).click(function(){
  if(status[apInd].circleIndex!=status[apInd].numApPic){
    status[apInd].Picture.fadeOut({duration: 1000, queue: false}).animate({left: '-2000px'},1000);
    status[apInd].NextPicture.fadeIn({duration: 1000, queue:false}).animate({left: '0px'},1000);
    status[apInd].PreviousPicture=status[apInd].Picture;
    status[apInd].Picture=status[apInd].NextPicture;
    $("#apCircle"+apInd+"-"+status[apInd].circleIndex).attr("class","circle");
    status[apInd].circleIndex++;
    $("#apCircle"+apInd+"-"+status[apInd].circleIndex).attr("class","circleSelected");
    status[apInd].NextPicture=$("#apPicture"+apInd+"-"+(status[apInd].circleIndex+1));        
  }
  if(status[apInd].circleIndex===status[apInd].numApPic)                              //hiding/showing arrows when at the edge of the selection
  {
    status[apInd].arrowDisplay="left";
    $("#buttonRight"+apInd).css("opacity",0).css("cursor","default");
  }
  else
  {
    if(status[apInd].arrowDisplay!=="both")
    {
      status[apInd].arrowDisplay="both";
      $("#buttonRight"+apInd).css("opacity",1).css("cursor","pointer");
      $("#buttonLeft"+apInd).css("opacity",1).css("cursor","pointer");
    }
  }
});

What I need is for the page width to stay constant or, more specifically, that there be no zooming in mobile and no horizontal scroll bar on desktop.


回答1:


Use overflow: hidden (or, for more fine grained control, overflow-x: hidden) in your CSS.

For example, if this is the HTML of your page:

<body>
  <div id="page-wrap">
    Contents
  </div>
</body>

You can use overflow like so:

#page-wrap {
  overflow-x: hidden; // hides horizontal overflowing elements
  overflow-y: auto; // shows scrollbars if the content vertically overflows
}

Link to MDN




回答2:


Try to add an overflow:hidden on the span with id="apImgBox"



来源:https://stackoverflow.com/questions/55810481/how-do-i-stop-the-page-from-expanding-when-animating-an-element-so-it-slides-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!