简体   繁体   English

Ondrag事件在我的HTML页面底部创建空白

[英]Ondrag event creates white space at the bottom of my HTML page

I have some code that detects if the cursor is being dragged left or right which upon doing so, cycles through an image sequence (similar to that of the Hyundai genesis website and its rotating car). 我有一些代码可以检测光标是向左还是向右拖动,从而在一个图像序列中循环(类似于现代创世纪网站及其旋转的汽车)。

However, evertime the function is called, it creates white space at the bottom of the html page. 但是,每次调用该函数时,它都会在html页面的底部创建空白。 Any idea what might be causing this? 知道是什么原因造成的吗?

Here's the Javascript for loading the image sequence into the array: 这是将图像序列加载到数组中的Javascript:

var cache = [];
  function imgList(base,firstNum,lastNum) {
    var imageFunction;
      for(var i = firstNum;i <= lastNum; i++) {
        imageFunction = new Image();

    if(i <=9){ var EXT = '000'}
    else if(i <= 99){var EXT = '00'}
    else if(i <= 999){var EXT = '0'}
    else{var EXT = ''}

    imageFunction.src = base + "." + EXT + i + ".png";
    cache.push(imageFunction);
    console.log(cache.length);
   }

}

Here is the Javascript function that is called when the drag event occurs: 这是发生拖动事件时调用的Javascript函数:

var prevX = -1;
var i = 0;
var drgleft = 0;
var drgright = 0;

function sequence(event){

if(prevX == -1){
  prevX = event.pageX;
  return false;
  }  
  //drag left
 if(prevX > event.pageX){
   console.log('dragged left');
   drgleft++;
   if(drgleft == 2){
   drgleft = 0;
   i--;
   if(i < 0){
    i = 30; //for optimization reasons, input the cache.length value   manually (this avoids unnecessary errors in the console and laggy framerate as a result).
  } 
  document.getElementById("TheBigOne").src = cache[i].src; //use      console.log(i); as a method of verifying that the code is executing correctly
   }
  }
 else if(prevX < event.pageX){
 console.log('dragged right');
 drgright++;
 if(drgright == 2){
 drgright = 0;
 i++;
 if(i > 30){ //for optimization reasons, input the cache.length value manually (this avoids unnecessary errors in the console and laggy framerate as a result).
        i=0;
    }
document.getElementById("TheBigOne").src = cache[i].src;
    }
 }
 else{}
 prevX = event.pageX
 }

Here is the html: 这是html:

<div class="The_main_event" ondrag="sequence(event)" id="PlaneTime">

 <br/>

  <img src="file:///C:/Users/Foo/Desktop/Website/Web_aeroplane/Web%20Test.0031.png" id="TheBigOne" class="planepic">

 <br/>

</div>

If there is white space around an image, a small gap will appear underneath it as long as it is an inline element. 如果图像周围有空白,则只要它是嵌入式元素,它下面就会出现一个小间隙。 There are a few ways to fix it, but the easiest is changing it to a block level element. 有几种方法可以修复它,但最简单的方法是将其更改为块级元素。

Try this: 尝试这个:

.planepic {
  display: block;
}

Here's a demo 这是一个演示

 div { background: red; display: inline-block; border: 1px solid black; } img { max-width: 100px; } .block { display: block; } 
 <div><img src="https://3.bp.blogspot.com/-W__wiaHUjwI/Vt3Grd8df0I/AAAAAAAAA78/7xqUNj8ujtY/s1600/image02.png"></div> <div><img src="https://3.bp.blogspot.com/-W__wiaHUjwI/Vt3Grd8df0I/AAAAAAAAA78/7xqUNj8ujtY/s1600/image02.png" class="block"></div> 

Alright I found the solution to the issue was something completely unrelated. 好吧,我发现解决该问题的方法完全不相关。 There was a bit of javascript which was used to remove the issue of the image ghosting when the image was dragged: 有一些JavaScript可用来消除拖动图像时图像重影的问题:

document.getElementById("TheBigOne").addEventListener("dragstart", function(event) {

var crt = this.cloneNode(true);
crt.style.backgroundColor = "";
crt.style.opacity = "0"; /* or visibility: hidden, or any of the above */
document.body.appendChild(crt);
event.dataTransfer.setDragImage(crt, 0, 0);
}, false);

Once I removed this code, the issue ceased to occur... looking at this code now, I can understand why, I guess I shouldn't stay up so late and code rubbish like this. 一旦删除了这段代码,问题就不再发生了……现在看这段代码,我可以理解为什么,我想我不应该熬夜并且像这样编写垃圾代码。

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

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