【问题标题】:My images jumps when I scroll on the page当我在页面上滚动时,我的图像会跳动
【发布时间】:2020-04-20 23:39:20
【问题描述】:

希望有人能帮忙。

我在一个慈善网站上玩耍,但当我在页面上滚动时,图像一直在跳动。

As you can see here

有人知道为什么会这样吗?

忽略网站的其余部分,因为我仍在研究如何解决其他问题。

我对编码很陌生

HTML(我只添加了几张照片,有很多):

<!-- NAVIGATION -->
<table class="topnav" id="navigation" cellspacing="2">
   <tr>
     <td><a href="news.html"> Newsletters </a></td>
     <td><a href="events.html"> Fundraisers </a></td>
     <td><a href="index.html"> About us </a></td>
     <td><a href="widows.html"> The widows </a></td>
     <td><a href="helping.html"> What we do </a></td>
     <td><a href="donate.html"> Donate </a></td>
     <td><a href="contact.html"> Contact us </a></td>
     <td><a href="gallery.html"> Gallery </a></td>
   </tr>
</table>

<!-- GALLERY -->

<div id="myBtnContainer" class="btndiv">
   <button class="btn active" onclick="filterSelection('all')"> Show all</button>
   <button class="btn" onclick="filterSelection('widows')"> Photos of Widows</button>
   <button class="btn" onclick="filterSelection('videos')"> Videos of Widows </button>
   <button class="btn" onclick="filterSelection('events')"> Charity events</button>
</div>

<!-- Portfolio Gallery Grid -->
<div class="row">

   <div class="column widows" class="onecol">
     <div class="content">
       <img src="gallery/images/widows/16.png" alt="widows">
     </div>
   </div>

</div>

<div class="row">

   <div class="column widows">
     <div class="content">
       <img src="gallery/images/widows/20.png" alt="widows" style="width:100%">
     </div>
   </div>

   <div class="column widows">
      <div class="content">
         <img src="gallery/images/widows/3.png" alt="widows" style="width:100%">
      </div>
   </div>

   <div class="column widows">
       <div class="content">
          <img src="gallery/images/widows/4.png" alt="widows" style="width: 100%;">
       </div>
   </div>

</div>

CSS:

 /* NAVIGATION BAR */

 .topnav {
   background-color: #ff9f80;
   overflow: auto;
   width: 100%;
   font-family: Perpetua, sans-serif;
   font-size: 120%;
   padding-left: 10%;
   padding-right: 10%;
 }

 /* NAVIGATION LINKS */

 .topnav a {
   color: #8b0000;
   display: block;
   float: left;
   padding: 5% 4%;
   text-align: center;
   text-decoration: none;
 }

 /* STICKY NAVIGATION BAR */

 .sticky {
   position: fixed;
   top: 0;
   width: 100%;
 }

 .sticky + .content {
   padding-top: 102px;
 }

 /* CHANGE COLOR WHEN HOVERING OVER NAVIGATION BAR */

 .topnav a:hover {
   background-color: #ffc6b3;
   color: #8b0000;
 }

 /* FONT FOR NAVIGATION LINKS */

nav {
   font-family: Perpetua, sans-serif;
}

/* GALLERY */

.btndiv {
   text-align: center;
 }

* {
   box-sizing: border-box;
 }

 /* CENTER WEBSITE */
 .main {
    max-width: 1000px;
    margin: auto;
  }


 .row {
    margin: 8px -16px;
  }

  /* PADDING BETWEEN COLUMNS */
  .row,
  .row > .column {
    padding: 8px;
  }

  /* THREE EQUAL COLUMS NEXT TO EACH OTHER */
  .column {
     float: left;
     width: 33.33%;
     display: none; /* Hide columns by default */
   }

   /* CLEAR FLOATS AFTER ROWS */ 
   .row:after {
      content: "";
      display: table;
      clear: both;
    }

  /* CONTENT */
 .content {
    background-color: white;
    padding: 10px;
  }

 /* The "show" class is added to the filtered elements */
 .show {
    display: block;
 }

/* Style the buttons */
.btn {
   border: none;
   outline: none;
   padding: 12px 16px;
   background-color: #ff9f80;
   cursor: pointer;
   border-radius: 15px 50px;
   font-size: 100%;
   color: #8b0000;
 }

 /* Add a grey background color on mouse-over */
.btn:hover {
   background-color: #ffc6b3;
}

/* Add a dark background color to the active button */
.btn.active {
   background-color: #b32d00;
   color: white;
 }

 /* IMAGE RESIZING */

 .onecol {
   position: fixed;
   width: 150%;
   margin-left: auto;
   margin-right: auto;
   display: block;
   column-span: all;
  }

JavaScript:

filterSelection("all") // Execute the function and show all columns
function filterSelection(c) {
   var x, i;
   x = document.getElementsByClassName("column");
   if (c == "all") c = "";
// Add the "show" class (display:block) to the filtered elements,     and remove the "show" class from the elements that are not selected
 for (i = 0; i < x.length; i++) {
   w3RemoveClass(x[i], "show");
   if (x[i].className.indexOf(c) > -1) w3AddClass(x[i], "show");
  }
}

// Show filtered elements
function w3AddClass(element, name) {
   var i, arr1, arr2;
   arr1 = element.className.split(" ");
   arr2 = name.split(" ");
   for (i = 0; i < arr2.length; i++) {
      if (arr1.indexOf(arr2[i]) == -1) {
        element.className += " " + arr2[i];
      }
    }
  }

  // Hide elements that are not selected
  function w3RemoveClass(element, name) {
    var i, arr1, arr2;
    arr1 = element.className.split(" ");
    arr2 = name.split(" ");
    for (i = 0; i < arr2.length; i++) {
      while (arr1.indexOf(arr2[i]) > -1) {
        arr1.splice(arr1.indexOf(arr2[i]), 1); 
      }
     }
     element.className = arr1.join(" ");
    }

   // Add active class to the current button (highlight it)
   var btnContainer = document.getElementById("myBtnContainer");
   var btns = btnContainer.getElementsByClassName("btn");
   for (var i = 0; i < btns.length; i++) {
     btns[i].addEventListener("click", function(){
       var current = document.getElementsByClassName("active");
       current[0].className = current[0].className.replace("active", "");
       this.className += "active";
      });
    }

【问题讨论】:

  • 请包含用于导航的 html 和 css 以及用于正文、标题、图像、标题的 css 中 html 元素的定位,还包括用于在滚动到顶部时定位导航的任何 javascript页面。
  • 您好,感谢您的回复,我已将代码编辑到帖子中

标签: javascript html css image


【解决方案1】:

如果您能更彻底地解释您的问题,这可能会更有帮助。 据我了解,如果您希望图像在滚动时具有粘性,您可以使用 CSS 的 sticky 属性

img.sticky {
  position: -webkit-sticky; /* Safari */
  position: sticky;
  top: 0;
}

我遇到的另一个问题是,当您向下滚动时,您的表格标签中会包含一类粘性。 检查发生这种情况的代码。

【讨论】:

  • 您好,感谢您的回复,我会看看并尝试您的粘贴代码。
猜你喜欢
  • 2017-10-22
  • 2015-04-03
  • 1970-01-01
  • 2014-05-20
  • 1970-01-01
  • 2019-05-03
  • 2021-02-26
  • 1970-01-01
  • 2014-01-31
相关资源
最近更新 更多