javascript - 页面滚动时将 Div 元素从一个移动到另一个

标签 javascript jquery

我看到了一些与我正在寻找的相似但不完全相同的东西。

所以我想做的是将元素从一个父 div 内移动到另一个父 div,但只有在用户向下滚动页面一定量后才可以。因此,一旦用户到达页面上的某个点,元素就会移动到另一个点,然后在页面的最顶部淡入。

到目前为止,我已经能够创建 div 元素并让它显示在页面顶部,但只有在用户向下滚动 600 时才会显示。我现在需要做的是一旦这个元素出现移动其他 div页面上的元素出现在其中。不确定我是否解释得很好!

因此,如果您查看下面的代码,我现在要做的是在用户向下滚动并出现时将所有 div 类“Test”移动到“Top”元素内。然后,如果用户再次向上滚动,“Top”元素就会消失,“Test”div 会回到它的正常位置。

$(document).scroll(function() {
  var y = $(this).scrollTop();
  if (y > 600) {
    $('#Top').fadeIn();
  } else {
    $('#Top').fadeOut();
  }
});
#Top {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  border-bottom: 2px solid #f2f2f2;
  border-radius: 0;
  background-color: rgb(255, 255, 255);
  z-index: 9999;
  padding: 15px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div id="Top"></div>

<div class="Test">
  <h1 class="heading-title">Title Here</h1>
  <div class="product-price">Price Here</li>
    <div class="cart-container"><button type="button" id="button-cart" class="button">Add to Cart</button></div>
  </div>
</div>

最佳答案

您可以使用 .each() jQuery遍历所有 <div class="Test"> 的方法元素,然后使用 .appendTo()将它们中的每一个及其所有内容移动到其他元素。

我还更正了<div class="product-price">Price Here</li>对此 <div class="product-price">Price Here</div> .

代码如下:

注意:我故意给了 body高度 2000px所以我们可以在这里测试(运行代码片段)。

$(document).scroll(function()
{
  if($(window).width() >= 480)
  {
     var y = $(this).scrollTop();
     var div;
  
     if (y > 600)
     {
       // for each element with class Test
       $('div.Test').each(function()
       {
         $(this).appendTo('#Top'); // move the element and contents to #top
       });
    
       $('#Top').fadeIn();
     } 
     else 
     {
       $('div.Test').each(function()
       {
         $(this).appendTo('body');    // move to body
       });
    
       $('#Top').fadeOut();
     }
  }
});
body
{
  height:2000px;
}

#Top {
  display: block;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  border-bottom: 2px solid #f2f2f2;
  border-radius: 0;
  background-color: rgb(255, 255, 255);
  z-index: 9999;
  padding: 15px;
}
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>

<body>
  <div id="Top"></div>

  <div class="Test">
    <h1 class="heading-title">Title Here</h1>
    <div class="product-price">Price Here</div>
    <div class="cart-container">
      <input type="button" id="button-cart" class="button" value="Add to Cart" />
    </div>
</div>
</body>

</html>

关于javascript - 页面滚动时将 Div 元素从一个移动到另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48168192/

相关文章:

javascript - 无法从数组内的对象中删除空元素

javascript - 正则表达式按特定模式选择所有 URL

缺少分号时Javascript未定义的类函数

jquery - IE 中 .change 函数的问题

javascript - Facebook 社交插件无法在 PHP 中使用 JavaScript 分页?

javascript 无法启动函数或返回结果

javascript - Jquery - 使用 $(this) 获取文本输入的值

Jquery MultiSelect - 更改字体大小和宽度

jquery - 单击其他按钮时,如何使所有选定的按钮恢复为原始按钮?

php - 从 Couchdb 返回用户特定数据