简体   繁体   English

父div滚动到子div位置

[英]Parent div to scroll to child divs position

Here's what I'm trying to make happen: User clicks on child div '.box' and when they click it the scaling goes up to full size and the parent div will scroll up or down to fit the child div perfectly. 这是我要实现的目标:用户单击子div'.box',然后单击它,缩放比例将放大至全尺寸,并且父div将向上或向下滚动以完全适合子div。

What's currently happening the parent div doesn't scroll to child div's top position. 父div当前发生的情况不会滚动到子div的最高位置。 I've tried offsets for window and parent div and setting scrollTo but I think I may be missing a key component to this function. 我已经尝试过为window和父div设置偏移量并设置scrollTo,但是我想我可能缺少此功能的关键组件。

Try it here: http://codepen.io/daviddiefenderfer/pen/yOodJd 在这里尝试: http : //codepen.io/daviddiefenderfer/pen/yOodJd

HTML: HTML:

<div id="sidebar">
  Question Type:
  <select>
    <option>Text</option>
    <option>Slider</option>
    <option>Star</option>
  </select>
  <button id="zoom" onclick="getFullView()">Zoom Out</button>
</div>
<div id="artboard">
  <div class="artboard-container">
    <div class="box active">
      Please select question type
      <br>
      <br>
      <input type="text" placeholder="Question Title">
      <div class="answers" >
      </div>
    </div>
  </div>
</div>

CSS: CSS:

body, html{
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
}
#sidebar{
  padding: 10px;
  box-sizing: border-box;
  width: 15%;
  height: 100%;
  background-color: #ccc;
  float: left;
}
#artboard{
  box-sizing: border-box;
  width: 85%;
  height: 100%;
  float: right;
  position:relative;
}
.artboard-container{
  position: relative;
  height: 100%;
  width: 100%;
}
.box{
  padding: 50px;
  text-align: center;
  float: left;
  background-color: lightblue;
  height: 100%;
  transform: scale(.5);
  transition: transform .6s;
  cursor: pointer;
}
.box.active{
  transition: .6s curve-bezier (0, 1.08, .59, 1);
  transform: scale(1);
  position: fixed;
  top: 0;
  z-index: 1;
  cursor: default;
}
#zoom{
  position: absolute;
  left: 20px;
  bottom: 30px;
}

JS: JS:

$(document).ready(function(){
  $('.box').width($('#artboard').width());
})

$('.box').on('mouseup', function(){
  $(this).addClass('active');
  $('#artboard').css('overflow', 'hidden');
});

function getFullView(){
  $('.active').removeClass('active');
  $('#artboard').css('overflow', 'scroll');
}

If you use position:fixed , top & z-index then the active box will always appear at the top of the screen. 如果您使用position:fixedtopz-index则活动框将始终出现在屏幕顶部。

.box.active{
  transition: .6s curve-bezier (0, 1.08, .59, 1);
  transform: scale(1);
  top: 0;
  position: fixed;
  z-index: 1;
  cursor: default;
}

http://codepen.io/anon/pen/jqGOQN http://codepen.io/anon/pen/jqGOQN

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

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