简体   繁体   English

使用 jquery 切换图像

[英]Toggle images with jquery

I am working on a project where images move to each corner of the screen on button presses.我正在做一个项目,当按下按钮时,图像会移动到屏幕的每个角落。 I need these images to toggle from hidden to shown.我需要这些图像从隐藏切换到显示。

To do this I am using toggle() but the issue is that I need the ones that are showing to stay showing until toggled off.为此,我使用了toggle()但问题是我需要显示的那些一直显示直到关闭。 Currently when I click one it will show, but if I click another then the one previously shown will disappear and the one I clicked will show;目前,当我单击一个时它会显示,但是如果我单击另一个,则先前显示的将消失,而我单击的将显示; only one image will show at a time.一次只显示一张图片。 Any suggestions would be helpful.任何的意见都将会有帮助。

 $(document).ready(function() { var img = $('#MyImage'); $('#NWest').on('click', function() { img.css({ top: '0px', left: '0px', position: 'absolute' }); $("img").toggle(); }); }); $(document).ready(function() { var text = $('#MyText'); $('#NWest').on('click', function() { text.css({ top: '150px', left: '245px', position: 'absolute' }); }); }); $(document).ready(function() { var img = $('#MyImage'); $('#NEast').on('click', function() { img.css({ top: '0px', left: '75%', position: 'absolute' }); $("img").toggle(); }); }); $(document).ready(function() { var text = $('#MyText'); $('#NEast').on('click', function() { text.css({ top: '150px', left: '88%', position: 'absolute' }); }); }); $(document).ready(function() { var img = $('#MyImage'); $('#SEast').on('click', function() { img.css({ top: '560px', left: '75%', position: 'absolute' }); $("img").toggle(); }); }); $(document).ready(function() { var text = $('#MyText'); $('#SEast').on('click', function() { text.css({ top: '710px', left: '88%', position: 'absolute' }); }); }); $(document).ready(function() { var img = $('#MyImage'); $('#SWest').on('click', function() { img.css({ top: '560px', left: '0px', position: 'absolute' }); $("img").toggle(); }); }); $(document).ready(function() { var text = $('#MyText'); $('#SWest').on('click', function() { text.css({ top: '710px', left: '245px', position: 'absolute' }); }); });
 .container { position: relative; top: 0%; left: 0%; color: white; font-size: 20px; top: 30px; } .text { max-width: 20ch; position: absolute; top: 150px; left: 245px; transform: translate(-50%, -50%); } img { filter: grayscale(100%); }
 <input type="button" value="North West" id="NWest"> <input type="button" value="North East" id="NEast"> <input type="button" value="South East" id="SEast"> <input type="button" value="South West" id="SWest"> <a href='http://cis337-0217.cisdprogram.com/Index.html'>Return to Index</a><br> <div class="container"> <img id="MyImage" src="MyImage.jpg" alt="MyImage" style="width:25%" ;> <div class="text" id="MyText"> Lines of text. Lines of text. Lines of text. Lines of text. Lines of text. Lines of text. Lines of text. </div> </div> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

You need to act on the visibility style.您需要对可见性样式采取行动。

  • Put 4 images in your HTML.在您的 HTML 中放入 4 个图像。
  • Give them a class in your CSS for the position.在你的 CSS 中给他们一个职位的类。
  • For each element, you need to toggle, you can use this homemade function toggleImageVisible().对于每个元素,你需要切换,你可以使用这个自制的函数toggleImageVisible()。

The following snippet work for 2 buttons:以下代码段适用于 2 个按钮:

 $(document).ready(function() { $('#NWest').on('click', function() { toggleImageVisible('MyImageA'); }); $('#NEast').on('click', function() { toggleImageVisible('MyImageB'); }); }); function toggleImageVisible(id) { var img = document.getElementById(id); img.style.visibility = (img.style.visibility === 'visible' ? 'hidden': 'visible'); }
 .container { position: relative; top: 0%; left: 0%; color: white; font-size: 20px; top: 30px; } .text { max-width: 20ch; position: absolute; top: 150px; left: 245px; transform: translate(-50%, -50%); } img { filter: grayscale(100%); } .MyImageClassA{ top: 0px; left: 0px; position: absolute; visibility: hidden; } .MyImageClassB{ top: 0px; left: 75%; position: absolute; visibility: hidden; }
 <input type="button" value="North West" id="NWest"> <input type="button" value="North East" id="NEast"> <input type="button" value="South East" id="SEast"> <input type="button" value="South West" id="SWest"> <a href='http://cis337-0217.cisdprogram.com/Index.html'>Return to Index</a><br> <div class="container"> <img id="MyImageA" class="MyImageClassA" src="MyImage.jpg" alt="MyImage" style="width:25%" ;> <img id="MyImageB" class="MyImageClassB" src="MyImage.jpg" alt="MyImage" style="width:25%" ;> <div class="text" id="MyText"> Lines of text. Lines of text. Lines of text. Lines of text. Lines of text. Lines of text. Lines of text. </div> </div> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

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

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