【问题标题】:How to align text and images with on click of an image in javascript/jquery?如何将文本和图像与单击 javascript/jquery 中的图像对齐?
【发布时间】:2018-06-02 03:14:36
【问题描述】:

我有一个fiddle,我通过查看下面的屏幕截图复制了它:

屏幕截图和小提琴的工作方式是,如果我点击上述任何项目,则会发生以下变化

  1. 背景颜色变为绿色。
  2. 图标和文本变为白色。

为了实现这一点,我使用的示例 JQuery 代码是:

$("#franchisehub").click(function() {
  if ($('.franchisehubtv').css('display') == "flex") {
    $('.arrow-down').css('display', 'none');
    $('.franchisehubtv').css('display', 'none');
    $('#franchisehub img.black').css('display', 'none');

  } else {
    resetAll();
    $('#franchisehub').css('background-color', 'green');
    $('#franchisehub p').css('color', 'white');
    $('.franchisehubtv').css('display', 'flex');
    $('#franchisehub img.black').css('display', 'none');
    $('#franchisehub img.white').css('display', 'inline-block');

  }

});


问题陈述:

在小提琴中,当我点击任何项目时,文本 (出现在方形框中) 会出现在框的中心,而 当它没有被点击时出现在方框之外(我不想发生这种情况)

我想知道我需要在小提琴中进行哪些更改,以便在单击项目和未单击项目时,它应该保持在同一位置。

【问题讨论】:

  • 似乎两个图像都在未选择框时显示,这会将文本向下推。你的resetAll() 函数中有什么?另外,我建议使用更通用的选择器,这样您就不需要重复自己了。见DRY @ jQuery.com
  • @showdev 为了使我的代码模块化,我使用了 resetAll() 函数,这样我就不必编写代码行了。是的,显示两个图像,当它被点击的白色被显示时,当它未被点击的黑色被显示时。
  • 我的意思是当没有选择一个框时,两个图像都会显示。我假设您想在两个图像之间切换。
  • @showdev 是对的。您应该隐藏第二张图片,然后在单击时显示它。
  • @DaleNguyen 有什么办法,我们可以用当前的代码吗?

标签: javascript jquery html css


【解决方案1】:

这是一个很长的答案,所以我把它放在这里。

在原始 CSS 中,您应该首先隐藏第二张图片

.white{
 display: none;
}

然后,在脚本中,您可以在单击时隐藏 .black:

 $("#franchisehub").click(function() {
      if ($('.franchisehubtv').css('display') == "flex") {
        $('.arrow-down').css('display', 'none');
        $('.franchisehubtv').css('display', 'none');
        $('#franchisehub img.white').css('display', 'inline-block');   
        $('#franchisehub img.black').css('display', 'none');

      } else {
        resetAll();
        $('#franchisehub').css('background-color', 'green');
        $('#franchisehub p').css('color', 'white');
        $('.franchisehubtv').css('display', 'flex');
        $('#franchisehub img.black').css('display', 'none');
        $('#franchisehub img.white').css('display', 'inline-block');

      }

在你的 resetAll() 中,你也应该有这个:

  $('#franchisehub img.black').css('display', 'inline-block');
  $('#franchisehub img.white').css('display', 'none');

但是你的代码有这么多重复,你应该想办法减少代码量。这个功能应该缩短很多! https://jsfiddle.net/866bh6du/1/

【讨论】:

  • 点击后文字好像又往下掉了。
  • 我不能为你做所有的事情,看看第一个盒子!
  • 没关系。您可以将修复应用到其他框,并尽可能找到使代码干净的方法。
  • 我清理了很多 CSS。没有时间清理 jquery 和 html 代码。很快就会做。
  • 你只需要为你的第二个盒子设置原始的CSS,它会很好。 #cloudbasedmobile .white{ 显示:内联块; } #cloudbasedmobile .black{ 显示:无; }
【解决方案2】:

当一个框未被选中时,这两个图像都会显示,这会将文本向下推。
另外,我建议不要使用内联 CSS 样式并使用更通用的选择器来保留它DRY

这是一个例子:

jQuery('.product').on('click', function() {
  jQuery(this).toggleClass('selected');
});
.product-all-contents {
  background-color: #f0f0f0;
}

.product-contents {
  margin-left: 15%;
  margin-right: 15%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem;
}

.product-contents .product {
  width: 10%;
  text-align: center;
  height: 150px;
  padding-top: 1%;
  padding-left: 1%;
  padding-right: 1%;
  border-style: solid;
  border-width: 3px;
  border-color: rgb(145, 147, 150);
  background-color: white;
  border-radius: 10px;
  font-size: 15px;
  font-family: 'Roboto';
  line-height: 1.2;
  color: rgb(58, 59, 60);
}

.product.selected {
  background-color: green;
  color: white;
}

.product img.white {
  display: none;
}
.product img.black {
  display: block;
}

.product.selected img.white {
  display: block;
}
.product.selected img.black {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="product-all-contents">
  <div class="product-contents">
    <div class="product" id="franchisehub">
      <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/Franchise-Hub.png" alt="" width="59" height="59" class="aligncenter size-full wp-image-7942">
      <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/franchise-hub-white.png" alt="" width="59" height="59" class="aligncenter size-full wp-image-7942">

      <p>Franchise Hub</p>
    </div>

    <div class="product" id="cloudbasedmobile">
      <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/cloud-grey.png" alt="" width="70" height="50" class="aligncenter size-full wp-image-8042" />
      <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/cloud-based-mobile-white.png" alt="" width="70" height="50" class="aligncenter size-full wp-image-8042" />

      <p>Cloud Based & Mobile</p>
    </div>
    <div class="product" id="businessanalytics">
      <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/business-analytics.png" alt="" width="53" height="53" class="aligncenter size-full wp-image-7949" />
      <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/business-analytics-white.png" alt="" width="53" height="53" class="aligncenter size-full wp-image-7949" />

      <p>Business Analytics</p>
    </div>
    <div class="product" id="techsupport">
      <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/tech-support.png" alt="" width="54" height="54" class="aligncenter size-full wp-image-7953" />
      <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/tech-support-white.png" alt="" width="54" height="54" class="aligncenter size-full wp-image-7953" />

      <p>Tech Support</p>
    </div>
    <div class="product" id="ordermanagement">
      <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/order-management.png" alt="" width="43" height="52" class="aligncenter size-full wp-image-7952" />
      <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/order-management-white.png" alt="" width="43" height="52" class="aligncenter size-full wp-image-7952" />

      <p>Order Management</p>
    </div>
    <div class="product" id="employeemanagement">
      <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/employee-management.png" alt="" width="59" height="59" class="aligncenter size-full wp-image-7942">
      <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/employee-management-white.png" alt="" width="59" height="59" class="aligncenter size-full wp-image-7942">
      <p style=" font-size: 15px;
         font-family: 'Roboto';line-height:1.2; margin-left: 5%;
         color: rgb(58, 59, 60);">Employee Management</p>
    </div>
    <div class="product" id="whitelabel">
      <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/white-label.png" alt="" width="59" height="59" class="aligncenter size-full wp-image-7942">
      <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/white-label-white.png" alt="" width="59" height="59" class="aligncenter size-full wp-image-7942">
      <p>White Label</p>
    </div>
  </div>
  <div class="product-contents">
    <div class="product" id="brandcontrol">
      <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/brand-control.png" alt="" width="57" height="57" class="aligncenter size-full wp-image-7956" />
      <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/brand-control-white.png" alt="" width="57" height="57" class="aligncenter size-full wp-image-7956" />

      <p>Brand Control</p>
    </div>
    <div class="product" id="leadtracking">
      <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/lead-tracking.png" alt="" width="50" height="50" class="aligncenter size-full wp-image-7957" />
      <img class="white" style="display:none;" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/lead-tracking-white.png" alt="" width="50" height="50" class="aligncenter size-full wp-image-7957" />

      <p>Lead Tracking &amp; CRM</p>
    </div>
    <div class="product" id="custominvoicing">
      <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/custom-invoicing.png" alt="" width="51" height="50" class="aligncenter size-full wp-image-7958" />
      <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/custom-invoicing-white.png" alt="" width="51" height="50" class="aligncenter size-full wp-image-7958" />

      <p>Custom Invoicing</p>
    </div>
    <div class="product" id="goalsetting">
      <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/goal-setting.png" alt="" width="50" height="51" class="aligncenter size-full wp-image-7959" />
      <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/goal-setting-white.png" alt="" width="50" height="51" class="aligncenter size-full wp-image-7959" />

      <p>Goal Setting</p>
    </div>
    <div class="product" id="customizationtools">
      <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/customization-tools.png" alt="" width="54" height="53" class="aligncenter size-full wp-image-7960" />
      <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/customization-tools-white.png" alt="" width="54" height="53" class="aligncenter size-full wp-image-7960" />

      <p>Customization Tools</p>
    </div>
    <div class="product" id="royaltycalculator">
      <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/royalty-calculator.png" alt="" width="50" height="51" class="aligncenter size-full wp-image-7961" />
      <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/royalty-calculator-white.png" alt="" width="50" height="51" class="aligncenter size-full wp-image-7961" />

      <p>Royalty Calculator</p>
    </div>
    <div class="product" id="emailmarketing">
      <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/email-marketing.png" alt="" width="51" height="52" class="aligncenter size-full wp-image-7962" />
      <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/email-marketing-white.png" alt="" width="51" height="52" class="aligncenter size-full wp-image-7962" />

      <p>Email Marketing</p>
    </div>
  </div>
</div>

只是为了好玩,这里有一个纯 CSS 没有 jQuery 的例子:

.product-all-contents {
  background-color: #f0f0f0;
}

.product-contents {
  display: flex;
  justify-content: center;
  align-items: stretch;
  flex-wrap: wrap;
}

.product-contents label {
  position: relative;
  flex: 0 0 20%;
}

.product {
  height: 100%;
  width: 100%;
  text-align: center;
  font-size: 15px;
  font-family: 'Roboto';
  line-height: 1.2;
  color: rgb(58, 59, 60);
  padding: 1%;
  background-color: white;
  border: 3px solid rgb(145, 147, 150);
  border-radius: 10px;
  box-sizing: border-box;
}

.product img.white {
  display: none;
}
.product img.black {
  display: inline-block;
}

.toggler {
  display: none;
}

.toggler:checked+.product {
  background-color: green;
  color: white;
}
.toggler:checked+.product img.white {
  display: inline-block;
  ;
}
.toggler:checked+.product img.black {
  display: none;
}
<div class="product-all-contents">
  <div class="product-contents">

    <label>
      <input type="checkbox" class="toggler">
      <div class="product">
        <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/Franchise-Hub.png" alt="" width="59" height="59">
        <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/franchise-hub-white.png" alt="" width="59" height="59">
        <p>Franchise Hub</p>
      </div>
    </label>

    <label>
      <input type="checkbox" class="toggler">
      <div class="product">
        <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/cloud-grey.png" alt="" width="70" height="50">
        <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/cloud-based-mobile-white.png" alt="" width="70" height="50">
        <p>Cloud Based &amp; Mobile</p>
      </div>
    </label>

    <label>
      <input type="checkbox" class="toggler">
      <div class="product">
        <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/business-analytics.png" alt="" width="53" height="53">
        <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/business-analytics-white.png" alt="" width="53" height="53">
        <p>Business Analytics</p>
      </div>
    </label>

    <label>
      <input type="checkbox" class="toggler">
      <div class="product">
        <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/tech-support.png" alt="" width="54" height="54">
        <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/tech-support-white.png" alt="" width="54" height="54">
        <p>Tech Support</p>
      </div>
    </label>

    <label>
      <input type="checkbox" class="toggler">
      <div class="product">
        <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/order-management.png" alt="" width="43" height="52">
        <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/order-management-white.png" alt="" width="43" height="52">
        <p>Order Management</p>
      </div>
    </label>

    <label>
      <input type="checkbox" class="toggler">
      <div class="product">
        <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/employee-management.png" alt="" width="59" height="59">
        <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/employee-management-white.png" alt="" width="59" height="59">
        <p>Employee Management</p>
      </div>
    </label>

    <label>
      <input type="checkbox" class="toggler">
      <div class="product">
        <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/white-label.png" alt="" width="59" height="59">
        <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/white-label-white.png" alt="" width="59" height="59">
        <p>White Label</p>
      </div>
    </label>

    <label>
      <input type="checkbox" class="toggler">
      <div class="product">
        <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/brand-control.png" alt="" width="57" height="57">
        <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/brand-control-white.png" alt="" width="57" height="57">

        <p>Brand Control</p>
      </div>
    </label>

    <label>
      <input type="checkbox" class="toggler">
      <div class="product">
        <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/lead-tracking.png" alt="" width="50" height="50">
        <img class="white" style="display:none;" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/lead-tracking-white.png" alt="" width="50" height="50">
        <p>Lead Tracking &amp; CRM</p>
      </div>
    </label>

    <label>
      <input type="checkbox" class="toggler">
      <div class="product">
        <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/custom-invoicing.png" alt="" width="51" height="50">
        <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/custom-invoicing-white.png" alt="" width="51" height="50">
        <p>Custom Invoicing</p>
      </div>
    </label>

    <label>
      <input type="checkbox" class="toggler">
      <div class="product">
        <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/goal-setting.png" alt="" width="50" height="51">
        <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/goal-setting-white.png" alt="" width="50" height="51">
        <p>Goal Setting</p>
      </div>
    </label>

    <label>
      <input type="checkbox" class="toggler">
      <div class="product">
        <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/customization-tools.png" alt="" width="54" height="53">
        <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/customization-tools-white.png" alt="" width="54" height="53">
        <p>Customization Tools</p>
      </div>
    </label>

    <label>    
      <input type="checkbox" class="toggler">
      <div class="product">
        <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/royalty-calculator.png" alt="" width="50" height="51">
        <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/royalty-calculator-white.png" alt="" width="50" height="51">
        <p>Royalty Calculator</p>
      </div>
    </label>

    <label>
      <input type="checkbox" class="toggler">
      <div class="product">
        <img class="black" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/05/email-marketing.png" alt="" width="51" height="52">
        <img class="white" src="https://thebettersoftwarecompany.com/wp-content/uploads/2018/06/email-marketing-white.png" alt="" width="51" height="52">
        <p>Email Marketing</p>
      </div>
    </label>

  </div>
</div>

【讨论】:

  • 现在看起来不错。你给了我周末项目。将尝试使用切换类来看看它是如何完成的。
【解决方案3】:

如果您可以共享 HTML 代码,那就太好了,无论如何,这里是您可以通过简单的方式实现这一点的方法......

$(".main-div").on('click', function() {
  $(this).css('background-color', 'green');
  $(this).css('color', 'white');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="main-div">
  Some text
</div>
<div class="main-div">
  Some text
</div>
<div class="main-div">
  Some text
</div>

【讨论】:

    猜你喜欢
    • 2012-09-29
    • 2021-01-24
    • 2011-01-14
    • 1970-01-01
    • 2016-06-19
    • 2023-03-14
    • 1970-01-01
    相关资源
    最近更新 更多