在下面的javascript代码中,我在html中有一个复选框。当我单击复选框时,一个文本框向下滑动,然后输入我的文本。当我再次单击复选框时,文本框将消失。见截图

这是我的密码。
const openModal = document.getElementById('mark-as-gift');
const modalBg = document.querySelector('.addtnew');
openModal.addEventListener('click', openModalBtn);
function openModalBtn() {
modalBg.classList.toggle('menscart2');
}
当我第二次单击复选框时,我想做的是3秒延迟。原因是当我第一次单击复选框时,文本区域和“添加到购物车”按钮一起移动。但是,当第二次单击复选框以关闭文本区域时,add按钮的移动速度要快于文本区域,并且重叠在文本区域的顶部,然后文本区域消失。我不想那样。我想要的是,当我单击复选框时,文本区域和“添加到车”按钮一起移动。谢谢
以下是Shopify and和HTML代码
{% if current_variant.available %}
<!-- GIFT NOTE -->
<div class="box-mens-wrap1">
<div class="mark-gift-wrapper mark-gift-wrapper1">
<input type="hidden" name="properties[Mark As Gift]" value="No" style="display:inline-block">
<input id="mark-as-gift" type="checkbox" name="properties[Mark As Gift]" value="Yes" style="display:inline-block; vertical-align:">
<label for="mark-as-gift" style="margin:0;display: inline-block;font-size:1em;font-family: 'proxima-nova', sans-serif !important; padding-top: 10px;"><ls-static-482034>{{'products.product.gift_text_1' | t}}</ls-static-482034></label>
<div class="line-item-property__field gift_note gift_note1">
<label for="Gift Note" class="closegift"></label>
<textarea class="textgift" placeholder="{{'products.product.note_text' | t}}" maxlength="250" id="Gift Note" name="properties[Gift Note]" style="width: 100%;"></textarea>
</div>
</div>
<div class="small--one-third medium-up--one-fifth mensqty">
{%- if settings.quantity_enable and current_variant.available -%}
<div class="product__quantity product__quantity--{{ settings.variant_type }}">
{%- render 'quantity-input', id: section_id, qty: 1, min: 1 -%}
</div>
{%- endif -%}
</div>
</div>
<!-- END gift not -->
{% else %}
<div class="prepend-2 append-2 text-center" style="background:#333;padding:10px;">
<span style="font-size:14px;font-family:'Futura' !important;color:#ccc;"><ls-static-197559>Out of Stock</ls-static-197559></span>
</div>
{%- endif -%}
<div id="form-actions" class="grid">
<div class="grid__item small--two-thirds medium-up--four-fifths menscart">
{%- liquid
assign default_text = 'products.product.add_to_cart' | t
assign button_text = 'products.product.add_to_cart' | t
if template == 'product.preorder'
assign default_text = 'products.product.preorder' | t
assign button_text = 'products.product.preorder' | t
endif
unless current_variant.available
assign button_text = 'products.product.sold_out' | t
endunless
-%}
{% if current_variant.available %}
<button type="button"
name="add"
id="AddToCart-{{ section_id }}"
class="btn btn--full add-to-cart{% if enable_dynamic_buttons %} btn--secondary{% endif %} addtnew"
style="background-color: #ffffff !important; width: 100% !important; border: 1px solid #ffffff !important;"
{% unless current_variant.available %} disabled="disabled"{% endunless %}
>
<span id="AddToCartText-{{ section_id }}" data-default-text="{{ default_text }}" class="addtocartknew">
{{ button_text }}
</span>
</button>
{%- endif -%}
</div>
</div>
<!-- Extend -- Add offer element -->
<div id="extend-offer" class="extend-offer-mens hide"></div>
<!-- Extend -- End Extend code -->
<textarea id="VariantsJson-{{ section_id }}" class="hide" aria-hidden="true" aria-label="Product JSON">
{{ product.variants | json }}
</textarea>
{%- if product.options.size > 1 -%}
<textarea id="CurrentVariantJson-{{ section_id }}" class="hide" aria-hidden="true" aria-label="Variant JSON">
{{ current_variant | json }}
</textarea>
{%- endif -%}
{%- endform -%}
</div>
<!-- Extend -- Load product integration script -->
{% render 'extend-product-integration' %}
<!-- Extend -- End Extend code -->
<style>
.mark-gift-wrapper {
margin-bottom: 10px;
}
.box-mens-wrap1 {
display: flex !important;
flex-direction: row-reverse;
justify-content: space-between;
width: 100%;
margin: 0px;
}
.mark-gift-wrapper1 {
outline: 1px solid #fff;
width: 100%;
height: 43.5px;
margin-left: 15px;
}
.menscart {
width: 100%;
padding-bottom: 20px;
}
.menscart2 {
margin-top: 120px;
transition-duration: 2s;
}
.gift_note1 {
margin-top: 10px;
width: 100%;
}
.textgift2 {
border: none !important;
}
@media (max-width: 768px) {
.mark-gift-wrapper1 {
margin-left: 10px;
}
.menscart2 {
margin-top: 110px;
}
}
</style>
发布于 2022-05-16 06:15:50
我不确定它是否会起作用,因为我没有在代码中看到文本区域元素消失(我认为缺少一些东西),而且我现在无法在Shopify上检查它。但是,可以这样想:当您向按钮分配一个新类时,立即使用transition-duration (换句话说:延迟),因此它的移动速度比正常慢,但是当您再次切换它(删除menscart2类名)时,延迟根本不存在,这可能会导致按钮正常移动(或以与以前不同的速度)。
尝试将其添加到CSS代码中:
.addtnew {
transition-duration: 2s;
}
https://stackoverflow.com/questions/72254449
复制相似问题