javascript-OnClick函数无法正常工作以重新定位内容

前端之家收集整理的这篇文章主要介绍了javascript-OnClick函数无法正常工作以重新定位内容 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我当前的代码有两个问题.

我正在尝试创建一个onclick事件,当单击#moreinfo时,该.maincontentcontainer会重新定位.

但是,当我第二次单击它时,它不会返回到它的原始位置,并且当我第三次,第四次单击等时,它不起作用.

任何帮助都会很棒

  1. $(document).ready(function() {
  2. var moved = false;
  3. $("#moreinfo").click(function() {
  4. if ($(".maincontentcontainer").attr("trigger") === "0") {
  5. $(".maincontentcontainer img,.maincontentcontainer h3").animate({right: "50%"});
  6. $(".maincontentcontainer").attr("trigger","1");
  7. } else {
  8. $(".maincontentcontainer img,.maincontentcontainer h3").animate({left: "50%"});
  9. $(".maincontentcontainer").attr("trigger","0");
  10. }
  11. });
  12. });
  1. .container {
  2. height: 100vh;
  3. }
  4. .contentcontainer {
  5. position: relative;
  6. height: inherit;
  7. bottom: 0;
  8. }
  9. .maincontentcontainer {
  10. display: -webkit-Box;
  11. display: -ms-flexBox;
  12. display: flex;
  13. -ms-flex-wrap: wrap;
  14. flex-wrap: wrap;
  15. -webkit-Box-align: center;
  16. -ms-flex-align: center;
  17. align-items: center;
  18. -webkit-Box-pack: center;
  19. -ms-flex-pack: center;
  20. justify-content: center;
  21. height: 100vh;
  22. }
  23. .maincontentcontainer img {
  24. position: absolute;
  25. top: 20%;
  26. -o-object-fit: cover;
  27. object-fit: cover;
  28. -o-object-position: bottom;
  29. object-position: bottom;
  30. height: 60%;
  31. width: 60%;
  32. opacity: 1;
  33. -webkit-transition: opacity .25s;
  34. transition: opacity .25s;
  35. -webkit-filter: grayscale(100%);
  36. /* Safari 6.0 - 9.0 */
  37. filter: grayscale(100%);
  38. }
  39. .maincontentcontainer h3 {
  40. position: absolute;
  41. top: 45%;
  42. -o-object-fit: cover;
  43. object-fit: cover;
  44. -o-object-position: bottom;
  45. object-position: bottom;
  46. height: 60%;
  47. width: 60%;
  48. opacity: 1;
  49. -webkit-transition: opacity .25s;
  50. transition: opacity .25s;
  51. z-index: 5;
  52. color: #ffd700;
  53. font-size: 200%;
  54. font-family: Lato;
  55. text-align: center;
  56. }
  57. .bottom {
  58. position: absolute;
  59. bottom: 45px;
  60. width: 100%;
  61. /* float: left; */
  62. line-height: 1;
  63. display: flex;
  64. justify-content: space-between;
  65. }
  66. #one {
  67. background-color: #ffd700;
  68. }
  1. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  2. <div id="one" class="container">
  3. <div class="contentcontainer">
  4. <div class="maincontentcontainer" trigger="0">
  5. <h3>Test</h3>
  6. <img src="https://static1.squarespace.com/static/5463c1d9e4b065276e6675d3/t/5ae6d699562fa7d975d137ce/1525077665893/El+Cap.jpg"></div>
  7. </div>
  8. <div class="bottom">
  9. <h3 id="moreinfo" class="header">More info</h3>
  10. </div>
  11. </div>
  12. </div>
最佳答案
为了解决您的原始问题,您在else语句中将图像推得太远了.左移:50%左移:20%将图像移回原始位置.

此外,为了能够重复单击它,您需要重置图像的位置.您可以通过在左侧动画上添加left:0,在左侧动画上添加right:0来实现.

  1. $(document).ready(function() {
  2. var moved = false;
  3. $("#moreinfo").click(function() {
  4. if ($(".maincontentcontainer").attr("trigger") === "0") {
  5. $(".maincontentcontainer img,.maincontentcontainer h3").animate({right: "50%",left:0});
  6. $(".maincontentcontainer").attr("trigger",.maincontentcontainer h3").animate({left: "20%",right:0});
  7. $(".maincontentcontainer").attr("trigger","0");
  8. }
  9. });
  10. });
  1. .container {
  2. height: 100vh;
  3. }
  4. .contentcontainer {
  5. position: relative;
  6. height: inherit;
  7. bottom: 0;
  8. }
  9. .maincontentcontainer {
  10. display: -webkit-Box;
  11. display: -ms-flexBox;
  12. display: flex;
  13. -ms-flex-wrap: wrap;
  14. flex-wrap: wrap;
  15. -webkit-Box-align: center;
  16. -ms-flex-align: center;
  17. align-items: center;
  18. -webkit-Box-pack: center;
  19. -ms-flex-pack: center;
  20. justify-content: center;
  21. height: 100vh;
  22. }
  23. .maincontentcontainer img {
  24. position: absolute;
  25. top: 20%;
  26. -o-object-fit: cover;
  27. object-fit: cover;
  28. -o-object-position: bottom;
  29. object-position: bottom;
  30. height: 60%;
  31. width: 60%;
  32. opacity: 1;
  33. -webkit-transition: opacity .25s;
  34. transition: opacity .25s;
  35. -webkit-filter: grayscale(100%);
  36. /* Safari 6.0 - 9.0 */
  37. filter: grayscale(100%);
  38. }
  39. .maincontentcontainer h3 {
  40. position: absolute;
  41. top: 45%;
  42. -o-object-fit: cover;
  43. object-fit: cover;
  44. -o-object-position: bottom;
  45. object-position: bottom;
  46. height: 60%;
  47. width: 60%;
  48. opacity: 1;
  49. -webkit-transition: opacity .25s;
  50. transition: opacity .25s;
  51. z-index: 5;
  52. color: #ffd700;
  53. font-size: 200%;
  54. font-family: Lato;
  55. text-align: center;
  56. }
  57. .bottom {
  58. position: absolute;
  59. bottom: 45px;
  60. width: 100%;
  61. /* float: left; */
  62. line-height: 1;
  63. display: flex;
  64. justify-content: space-between;
  65. }
  66. #one {
  67. background-color: #ffd700;
  68. }
  69. #moreinfo{
  70. z-index:10;
  71. }
  1. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  2. <div id="one" class="container">
  3. <div class="contentcontainer">
  4. <div class="maincontentcontainer" trigger="0">
  5. <h3>Test</h3>
  6. <img src="https://static1.squarespace.com/static/5463c1d9e4b065276e6675d3/t/5ae6d699562fa7d975d137ce/1525077665893/El+Cap.jpg"></div>
  7. </div>
  8. <div class="bottom">
  9. <h3 id="moreinfo" class="header">More info</h3>
  10. </div>
  11. </div>
  12. </div>

猜你在找的HTML相关文章