我使用jQuery UI向左滑动内容,然后同时滑动其他内容以取代第一个内容的位置.当我第一次按下"更改用户名"时,它可以正常工作.然而,当我想反转slideToggle的时候,我的新内容会向下推第一个内容,这会导致内容滑动然后弹出.我怎么才能避免这种情况呢?

我不希望图像也下滑.它应该留在自己的位置上.

position: absolute也不是一个选项.

$("#cu").click(function(){
  $(".profileBox").toggle('slide', { direction: 'left' }, 500);
  $("#changeName").toggle('slide', { direction: 'right' }, 500);
  $("#backer").toggle('slide', { direction: 'right' }, 500);
});
$("#backer").click(function(){
  $("#changeName").toggle('slide', { direction: 'right' }, 500);
  $("#backer").toggle('slide', { direction: 'right' }, 500);
  $(".profileBox").toggle('slide', { direction: 'left' }, 500);

});
#profile {
width: 75%;
max-width: 400px;
height: 100%;
background-color: rgba(37, 76, 91, 1);
position: fixed;
top: 0;
display: none;
right: 0;
}
#backer {
font-size: min(3.0vw, 22px);
position: absolute;
margin: 5px;
width: 100%;
display: none;
}
.superFlex, .profileBox {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
.usercon, #changeName {
width: 100%;
height: 40px;
display: flex;
background-color: #4E748B;
font-size: 14px;
margin-top: 2px;
cursor: pointer;
}
.profileBox{
width:100%;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>

<div id="profile" style="display: block;">
  <i id="backer" class="fa fa-arrow-left" aria-hidden="true" style="display: none;"></i>
    <div class="superFlex">
    <div class="first">
      <img src = "https://i.stack.imgur.com/eSStq.png?s=256&g=1" height = "100">       
      <p style="margin:0;">TEXT</p>
    </div>
    <form id="changeName" class="swiped" style="display: none;">
      <p class="general">Username</p>
        </form> 
    <div class="profileBox" style="">   
      <p class="general">Account settings</p>
          <div class="usercon" id="cu"><p>Change username</p></div>
      </div>    
    </div>
</div>

推荐答案

我认为jQuery UI's .toggle method不适合做这项工作..toggle()用于显示/隐藏单个元素,而不是将网格中的内容单元格向左或向右移动.

我认为正确的概念模型是想象一个滑块框,其宽度是视区的两倍.它包含两个并排的幻灯片,每个幻灯片的宽度都是滑块的一半(或与视区的宽度完全相同).一次只能看到一张幻灯片.导航按钮将更改滑块的margin-left属性,以便可以看到第一张或第二张幻灯片.

这将需要在一定程度上重新处理现有的HTML struct .例如,#backer箭头需要位于第二张幻灯片的内部.我们可以使用负margin-top来显示在您最初使用的大致相同的位置.

我们还需要将overflow: hidden添加到滑块的容器中-在本例中为.superFlex-这样看不到看不到的幻灯片.

生成的代码如下所示:

$('#cu').click(function() {
  $('#Slider').animate({
    marginLeft: '-100%'
  });
});

$('#backer').click(function() {
  $('#Slider').animate({
    marginLeft: '0'
  });
});
#profile {
  width: 75%;
  max-width: 400px;
  height: 100%;
  background-color: rgba(37, 76, 91, 1);
  position: fixed;
  top: 0;
  display: none;
  right: 0;
}

#backer {
  cursor: pointer;
  font-size: min(3.0vw, 22px);
  position: absolute;
  margin-left: 5px;
  margin-top: -155px;
}

.superFlex,
.profileBox {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}

.usercon,
#changeName {
  width: 100%;
  height: 40px;
  display: flex;
  background-color: #4E748B;
  font-size: 14px;
  margin-top: 2px;
  cursor: pointer;
}

.profileBox {
  width: 100%;
}

.slider {
  align-self: flex-start;
  display:flex;
  margin-left: 0;
  width: 200%;
}

.slide {
  width: 50%;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="profile" style="display: block;">
  <div class="superFlex" style="overflow:hidden;">
    <div class="first">
      <img src="https://i.stack.imgur.com/eSStq.png?s=256&g=1" height="100">
      <p style="margin:0;">TEXT</p>
    </div>
    
    <div class="slider" id="Slider">
      <div class="slide">
        <div class="profileBox">
          <p class="general">Account settings</p>
          <div class="usercon" id="cu">
            <p>Change username</p>
          </div>
        </div>
      </div>
      <div class="slide">
        <form id="changeName" class="swiped">
          <p class="general">Username</p>
        </form>
        <i id="backer" class="fa fa-arrow-left" aria-hidden="true"></i>
      </div>
    </div>
    
  </div>
</div>

Jquery相关问答推荐

在Click事件上将焦点设置为Kendo UI网格列筛选器文本框

JQuery AJAX数据表保留前导零

如何在 jquery 中切换 attr()

Jquery $(this) 子 Select 器

jQuery 或 JavaScript 中的$符号是什么意思?

Bootstrap 4 多选下拉菜单

对自定义属性执行客户端验证

jQuery 的 ajax 默认超时值是多少?

JSON 服务在失败/错误时应该返回什么

使用 jQuery 将行添加到表的 tbody

如何使用 jQuery 或 JavaScript 模拟单击​​按钮的动作?

jQuery 序列化不注册复选框

动画元素变换旋转

jQuery - 使用发布数据重定向

使用 时如何从 select2 中获取选定文本

如何使用 JSON、jQuery 将一组复杂对象发布到 ASP.NET MVC 控制器?

如何删除/更改 JQuery UI 自动完成助手文本?

如何在 jQuery 中 Select this中的元素?

Jquery live() 与委托()

Google 的 CDN 上的最新 jQuery 版本