如何防止单击时按钮移动?

JustAnotherSimpleProgrammer__

我有以下代码:

<!DOCTYPE html>
<html>
<body>
<head>
<script>


function Show(element){
element.style.display = "block";
}


function Hide(element){
element.style.display = "none";
}


function Slide(){

try{

img1 = img1id.style.display;
img2 = img2id.style.display;
img3 = img3id.style.display;

if(img1 =="none" && img2=="none"){
Show(img1id);
Hide(img3id);
}

else if(img2 == "block" && img1=="none"){
Hide(img2id)
Show(img3id);
}

else if(img3 == "block" && img1 == "none" && img2 == "none"){
Show(img1id);
Hide(img3id);	
}

else {
Hide(img1id);
Hide(img3id);
Show(img2id);
}

}

catch(e){
alert("Evento " + e);

}

}


var img1id;
var img2id;
var img3id;
var buttonid;


function gestoreLoad(){
try{

img1id = document.getElementById("id1");
img2id = document.getElementById("id2");
img3id = document.getElementById("id3");
buttonid = document.getElementById("id4");
buttonid.onclick = Slide;

}
catch(e) {
alert("gestoreLoad " + e);
}
}

window.onload = gestoreLoad;
</script>
<style type="text/css">

button:active {
  padding: 0px;
}

button#id4 {
	margin: auto auto auto auto;
	font-weight: bold;
	height: 80px;
	width: 150px;
	background-color: #000;
	color: #FFF;
	border-radius: 20px 20px 20px 20px;
	font-size: 1.5em;
}


img {
	 -webkit-animation: fadein 2s; /* Safari, Chrome and Opera > 12.1 */
       -moz-animation: fadein 2s; /* Firefox < 16 */
        -ms-animation: fadein 2s; /* Internet Explorer */
         -o-animation: fadein 2s; /* Opera < 12.1 */
            animation: fadein 2s;
}

@keyframes fadein {
    from { opacity: 0; }
    to   { opacity: 1; }
}

/* Firefox < 16 */
@-moz-keyframes fadein {
    from { opacity: 0; }
    to   { opacity: 1; }
}

/* Safari, Chrome and Opera > 12.1 */
@-webkit-keyframes fadein {
    from { opacity: 0; }
    to   { opacity: 1; }
}

/* Internet Explorer */
@-ms-keyframes fadein {
    from { opacity: 0; }
    to   { opacity: 1; }
}

/* Opera < 12.1 */
@-o-keyframes fadein {
    from { opacity: 0; }
    to   { opacity: 1; }
}
	</style>
</head>
<center>
<img id="id1" style="width:800px; height:400px;" src="https://image.shutterstock.com/image-photo/beatiful-nature-lake-forest-pang-260nw-1039096585.jpg" />
<img id="id2" style="display:none; width:800px; height:400px;" src="https://thumbs.dreamstime.com/b/fragment-traditional-iranian-architecture-against-beatiful-purple-sky-yellow-pink-clouds-beautiful-sunset-iranian-122642415.jpg" />
<img id="id3" style="display:none; width:800px; height:400px;" src="https://live.staticflickr.com/5808/21804482933_2b1279ef62_b.jpg" />
</center>

<br />
<center>
<button id="id4" /><span>Slideshow!</span></button>
</center>

</body>
</html>

我以互联网上的一些图像为例。顺便说一句,当我第一次单击按钮时(因此在页面加载后),它向上移动...您知道任何解决方案吗?

我在W3chools中知道的Ps可以完美地运行幻灯片显示的代码,但是我会自己尝试并进一步学习有关JavaScript的乐趣

Manjuboyz

这些是我观察到的事情:

  1. 您使用的按钮不正确,我也对此进行了修改。
  2. 您必须经过JavaScript基本编写方法,这就是按钮首次闪烁的原因,为什么?这是因为,当第一次单击事件发生时,它总是处于最后else状态,并且使第一张图像处于隐藏状态,而此时按钮开始向上移动,其他按钮show()将图像移回其位置。现在,如果您考虑为什么第二次和第三次点击没有发生,那是因为第一张图片现在具有displayasnone属性block

第一次,第一张图片的显示属性始终是null您可以通过添加alert()了解来对其进行验证,因此,为了解决我所做的问题,因为您希望在页面加载期间显示第一张图片,因此,我已经使用display:block;了内联查询中的第一张图像,因此当您第一次单击按钮时,现在返回的block是属性而不是返回null值,因此它停止了按钮的上移。

我想您已经得到我想要传达的信息,如果没有,我们可以联系。

下面是工作代码(随意修改)

function Show(element) {
  element.style.display = "block";
}


function Hide(element) {
  element.style.display = "none";
}


function Slide() {

  try {

    img1 = img1id.style.display;
    img2 = img2id.style.display;
    img3 = img3id.style.display;

    if (img1 === "none" && img2 == "none") {
      Show(img1id);
      Hide(img3id);
    } else if (img2 == "block" && img1 == "none") {
      Hide(img2id)
      Show(img3id);
    } else if (img3 == "block" && img1 == "none" && img2 == "none") {
      alert("none");
      Show(img1id);
      Hide(img3id);
    } else {
      Hide(img1id);
      Hide(img3id);
      Show(img2id);
    }

  } catch (e) {
    alert("Evento " + e);

  }

}


var img1id;
var img2id;
var img3id;
var buttonid;


function gestoreLoad() {
  try {

    img1id = document.getElementById("id1");
    img2id = document.getElementById("id2");
    img3id = document.getElementById("id3");
    buttonid = document.getElementById("id4");
    buttonid.onclick = Slide;

  } catch (e) {
    alert("gestoreLoad " + e);
  }
}

window.onload = gestoreLoad;
button:active {
  padding: 0px;
}

button#id4 {
  margin: auto auto auto auto;
  font-weight: bold;
  height: 80px;
  width: 150px;
  border-radius: 20px 20px 20px 20px;
  background-color: #000;
  color: #FFF;
  font-size: 1.5em;
}

img {
  -webkit-animation: fadein 2s;
  /* Safari, Chrome and Opera > 12.1 */
  -moz-animation: fadein 2s;
  /* Firefox < 16 */
  -ms-animation: fadein 2s;
  /* Internet Explorer */
  -o-animation: fadein 2s;
  /* Opera < 12.1 */
  animation: fadein 2s;
}

@keyframes fadein {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}


/* Firefox < 16 */

@-moz-keyframes fadein {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}


/* Safari, Chrome and Opera > 12.1 */

@-webkit-keyframes fadein {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}


/* Internet Explorer */

@-ms-keyframes fadein {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}


/* Opera < 12.1 */

@-o-keyframes fadein {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
<center>
  <img id="id1" style="display:block; width:800px; height:400px;" src="https://image.shutterstock.com/image-photo/beatiful-nature-lake-forest-pang-260nw-1039096585.jpg" />
  <img id="id2" style="display:none; width:800px; height:400px;" src="https://thumbs.dreamstime.com/b/fragment-traditional-iranian-architecture-against-beatiful-purple-sky-yellow-pink-clouds-beautiful-sunset-iranian-122642415.jpg" />
  <img id="id3" style="display:none; width:800px; height:400px;" src="https://live.staticflickr.com/5808/21804482933_2b1279ef62_b.jpg" />
</center>
<br />
<center>
  <button id="id4" type="button">Slideshow!</button>
</center>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

当按钮处于焦点状态时,如何防止Enter键触发单击事件

来自分类Dev

单击<a href=> </a>时,防止屏幕移动

来自分类Dev

单击登录按钮时如何防止页面下移?

来自分类Dev

单击按钮时防止div抖动

来自分类Dev

单击按钮时防止Javascript事件

来自分类Dev

单击按钮时如何防止表单提交?

来自分类Dev

单击标题中的按钮时,如何防止手风琴切换?

来自分类Dev

单击时如何禁用按钮?

来自分类Dev

当Firefox中的按钮处于活动状态时,如何防止按钮文字移动?

来自分类Dev

防止单击按钮时打开引导模态

来自分类Dev

如何防止单击时按钮移动?

来自分类Dev

单击上\下\左\右按钮时如何在div中移动圆

来自分类Dev

如何防止快速多次单击表单按钮

来自分类Dev

防止在单击按钮时关闭XUL notificationBox

来自分类Dev

如何使按钮在单击时消失

来自分类Dev

蚂蚁设计:如何在单击“折叠”标题但允许按钮时防止扩展?

来自分类Dev

每次单击按钮时如何移动画布对象?

来自分类Dev

防止单击按钮时打开VBE

来自分类Dev

单击登录按钮时如何防止页面下移?

来自分类Dev

单击按钮时如何防止表单提交?

来自分类Dev

编辑文字时如何防止徽标移动?

来自分类Dev

AspxGridView,单击更新按钮时如何防止更新?

来自分类Dev

移动iOS后单击时按钮消失

来自分类Dev

单击时如何删除按钮?

来自分类Dev

移动应用程序如何防止当我在此对话框外单击时未关闭确认框

来自分类Dev

单击按钮时如何获取按钮的ID?

来自分类Dev

单击“向上”和“向下”按钮时,如何使边框上下移动?

来自分类Dev

单击按钮时如何更改按钮颜色

来自分类Dev

连续第二次单击按钮时如何防止过滤并返回默认结果

Related 相关文章

  1. 1

    当按钮处于焦点状态时,如何防止Enter键触发单击事件

  2. 2

    单击<a href=> </a>时,防止屏幕移动

  3. 3

    单击登录按钮时如何防止页面下移?

  4. 4

    单击按钮时防止div抖动

  5. 5

    单击按钮时防止Javascript事件

  6. 6

    单击按钮时如何防止表单提交?

  7. 7

    单击标题中的按钮时,如何防止手风琴切换?

  8. 8

    单击时如何禁用按钮?

  9. 9

    当Firefox中的按钮处于活动状态时,如何防止按钮文字移动?

  10. 10

    防止单击按钮时打开引导模态

  11. 11

    如何防止单击时按钮移动?

  12. 12

    单击上\下\左\右按钮时如何在div中移动圆

  13. 13

    如何防止快速多次单击表单按钮

  14. 14

    防止在单击按钮时关闭XUL notificationBox

  15. 15

    如何使按钮在单击时消失

  16. 16

    蚂蚁设计:如何在单击“折叠”标题但允许按钮时防止扩展?

  17. 17

    每次单击按钮时如何移动画布对象?

  18. 18

    防止单击按钮时打开VBE

  19. 19

    单击登录按钮时如何防止页面下移?

  20. 20

    单击按钮时如何防止表单提交?

  21. 21

    编辑文字时如何防止徽标移动?

  22. 22

    AspxGridView,单击更新按钮时如何防止更新?

  23. 23

    移动iOS后单击时按钮消失

  24. 24

    单击时如何删除按钮?

  25. 25

    移动应用程序如何防止当我在此对话框外单击时未关闭确认框

  26. 26

    单击按钮时如何获取按钮的ID?

  27. 27

    单击“向上”和“向下”按钮时,如何使边框上下移动?

  28. 28

    单击按钮时如何更改按钮颜色

  29. 29

    连续第二次单击按钮时如何防止过滤并返回默认结果

热门标签

归档