javascript - 父 div 滚动到子 div 位置

标签 javascript jquery html css

这是我正在努力实现的目标: 用户点击子 div '.box',当他们点击它时,缩放比例会上升到完整大小,父 div 将向上或向下滚动以完美适合子 div。

当前发生的情况是父 div 没有滚动到子 div 的顶部位置。我尝试了窗口和父 div 的偏移量并设置了 scrollTo,但我认为我可能缺少此功能的关键组件。

在这里试试:http://codepen.io/daviddiefenderfer/pen/yOodJd

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:

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:

$(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');
}

最佳答案

如果您使用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

关于javascript - 父 div 滚动到子 div 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36339887/

相关文章:

javascript - 在带有单击事件的 li 中停止复选框被选中两次

php - 解析错误 : syntax error, 在第 1 行出现意外的 $end

jquery - 无法在 'readAsDataURL' : parameter 1 is not of type 'FileReader' 上执行 'Blob'

java - 使用 Jsoup 从网页中读取指定行的文本

javascript - 获取按钮文本值以确定变量值

javascript - 错误: [$injector:unpr] Unknown provider: RestangularProvider <- Restangular <- ctrlAG

javascript数组到表布局损坏

javascript - 使用 jquery 在选择菜单项时更改菜单项颜色

javascript - JQuery Animate 没有动画

php - 保存唯一的 "post_id"来定位div