隐藏和显示HTML元素使用CSS基于按钮点击。

huangapple go评论53阅读模式
英文:

Hide and show HTML elements using CSS based on button click

问题

Using vanilla HTML, Javascript, and preferably just pure CSS,我尝试实现一个功能,当用户点击按钮时,向用户显示接下来的4个元素。

由于在代码段中存在限制,样式不太明显-因为我已经实现了一个网格系统,所以实际上一行中有4个项目(它们只是水平的)。

默认情况下,我想只显示4个项目,但当用户选择“加载更多”时,接下来的4个项目会被显示,依此类推。按钮应始终位于最后一个可见元素的正下方。

是否有人可以指导或提供一个可以实现这个结果的方法?

英文:

Using vanilla HTML, Javascript and preferably just pure CSS I'm trying to implement a feature whereby when a user clicks a button, the next 4 elements are displayed to the user.

Due to limitations in the snippet, the styles aren't quite reflective - as I've implemented a grid system, so there's actually 4 items in a row (they're just horizontal in the above).

By default I want to show just 4 items, but when the user selects "load more", then the next 4 are displayed & so on. The button should always remain just below the last visible element.

Can anyone guide or reference an approach that would achieve this result?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.container {
  padding: 16px;
}

.items-container {
  flex-direction: row;
  display: flex;
  gap: 16px;
}

.item {
  flex: 0 0 auto;
  width: 25%;
  background-color: lightblue;
  height: 320px;
}

<!-- language: lang-html -->

&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;items-container&quot;&gt;
    &lt;div class=&quot;item&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;item&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;item&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;item&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;item&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;item&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;item&quot;&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;p&gt;Showing 4 of 10&lt;/p&gt;
  &lt;button&gt;
    Load more
  &lt;/button&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

这是你要的翻译:

"Is this what you are looking for? If you are only showing the 1st 4 elements at the start then the rest needs to be hidden until you press the button.

Once you press it, loop thru all hidden elements and remove the class which hides those elements d-none in this example and change your text from Showing 4 of 8 to Showing 8 of 8 with textContent

const load = document.getElementById("load");
const divs = document.getElementsByClassName("d-none");
const showing = document.getElementById("showing");
load.addEventListener("click", function(){
    Array.from(divs).forEach((ele) => {
        // Do stuff here
        ele.classList.remove("d-none");
    });
    showing.textContent = "Showing 8 of 8";
})
.container {
  padding: 16px;
}

.items-container {
  flex-direction: row;
  display: flex;
  gap: 16px;
}

.item {
  flex: 0 0 auto;
  width: 25%;
  background-color: lightblue;
  height: 320px;
}

.d-none {
  display: none;
}
<div class="container">
  <div class="items-container">
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item d-none"></div>
    <div class="item d-none"></div>
    <div class="item d-none"></div>
    <div class="item d-none"></div>
  </div>
  <p id="showing">Showing 4 of 8</p>
  <button id="load">
    Load more
  </button>
</div>
英文:

Is this what you are looking for? If you are only showing the 1st 4 elements at the start then the rest needs to be hidden until you press the button.

Once you press it, loop thru all hidden elements and remove the class which hiddes those elements d-none in this example and change your text from Showing 4 of 8 to Showing 8 of 8 with textContent

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const load = document.getElementById(&quot;load&quot;);
const divs = document.getElementsByClassName(&quot;d-none&quot;);
const showing = document.getElementById(&quot;showing&quot;);
load.addEventListener(&quot;click&quot;, function(){
	Array.from(divs).forEach((ele) =&gt; {
    // Do stuff here
    ele.classList.remove(&quot;d-none&quot;);
  });
  showing.textContent = &quot;Showing 8 of 8&quot;;
})

<!-- language: lang-css -->

.container {
  padding: 16px;
}

.items-container {
  flex-direction: row;
  display: flex;
  gap: 16px;
}

.item {
  flex: 0 0 auto;
  width: 25%;
  background-color: lightblue;
  height: 320px;
}

.d-none {
  display: none;
}

<!-- language: lang-html -->

&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;items-container&quot;&gt;
    &lt;div class=&quot;item&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;item&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;item&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;item&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;item d-none&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;item d-none&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;item d-none&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;item d-none&quot;&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;p id=&quot;showing&quot;&gt;Showing 4 of 8&lt;/p&gt;
  &lt;button id=&quot;load&quot;&gt;
    Load more
  &lt;/button&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 1

这是你想要做的吗?解释:updateCount将当前计数写入p元素。updateItems根据count的当前值更改项目的显示属性。按钮上的事件侦听器将当前计数的值增加4,然后调用updateItems函数,该函数根据当前计数更新样式,然后调用updateCount函数将当前页写入p。希望这有帮助!

英文:

Is this what you are trying to do? Explanation: updateCount writes the current count to the p element. updateItems changes the display property of your items based on the current value of count. The event listener on the button increases the value of the current count by 4 and then calls the updateItems function, which updates the styles based on the current count and then calls the updateCount function to write the current page to the p. I hope this helps!

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const items = Array.from(document.querySelectorAll(&#39;.item&#39;));
let count = 4;

function updateCount() {
  document.querySelector(&#39;p&#39;).innerHTML = &#39;Showing &#39; + count + &#39; of &#39; + items.length;
}

function updateItems() {
  for (let index = 0; index &lt; count; index++) {
    items[index].style.display = &#39;&#39;;
  }

  for (let index = count; index &lt; items.length; index++) {
    items[index].style.display = &#39;none&#39;;
  }

  updateCount();
}

updateItems();

document.querySelector(&#39;button&#39;).addEventListener(&#39;click&#39;, () =&gt; {
  count += 4;
  count = count &gt; items.length ? items.length : count;
  updateItems();
});

<!-- language: lang-css -->

.container {
  padding: 16px;
}

.items-container {
  flex-direction: row;
  display: flex;
  gap: 16px;
}

.item {
  flex: 0 0 auto;
  width: 25%;
  background-color: lightblue;
  height: 320px;
}

<!-- language: lang-html -->

&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;items-container&quot;&gt;
    &lt;div class=&quot;item&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;item&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;item&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;item&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;item&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;item&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;item&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;item&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;item&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;item&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;item&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;item&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;item&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;item&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;item&quot;&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;p&gt;&lt;/p&gt;
  &lt;button&gt;
    Load more
  &lt;/button&gt;
&lt;/div&gt;

<!-- end snippet -->

答案3

得分: 0

我假设你已经加载了这些元素,如果没有,我提供的解决方案可能不会完全奏效:

<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <title>Filter Testing</title>
    <style>
        .container {
            padding: 16px;
        }

        .items-container {
            flex-direction: row;
            display: flex;
            gap: 16px;
        }

        .item {
            flex: 0 0 auto;
            width: 25%;
            background-color: lightblue;
            height: 320px;
        }

        .hidden-item {
            display: 'none';
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="items-container">
            <div class="item"></div>
            <div class="item"></div>
            <div class="item"></div>
            <div class="item"></div>
            <div class="hidden-item"></div>
            <div class="hidden-item"></div>
            <div class="hidden-item"></div>
            <div class="hidden-item"></div>
            <div class="hidden-item"></div>
            <div class="hidden-item"></div>
            <div class="hidden-item"></div>
            <div class="hidden-item"></div>
            <div class="hidden-item"></div>
            <div class="hidden-item"></div>
            <div class="hidden-item"></div>
            <div class="hidden-item"></div>
        </div>
        <p>显示 4 个,共 10 个</p>
        <button id="loadMoreButton">
            加载更多
        </button>
    </div>
    <script>
        //保存按钮引用
        let loadMoreButton = document.getElementById('loadMoreButton');
        //保存隐藏项
        //创建一个数组进行迭代,因为querySelector返回的是节点列表
        function loadMoreItems() {
            let hiddenItems = document.querySelectorAll('.hidden-item');
            let displayedItems = document.querySelectorAll('.item');
            Array.from(hiddenItems).forEach(function (item, index) {
                //加载 4 个额外项
                //请记住数组索引从 0 开始,所以 3 是第四个加载的元素
                if (index < 4) {
                    //移除隐藏元素的类,并添加显示元素的类
                    item.classList.remove('hidden-item');
                    item.classList.add('item');
                }
            });
        }
        //将loadMoreItems添加到按钮上
        loadMoreButton.addEventListener('click', loadMoreItems);
    </script>

</body>

</html>
英文:

I suppose you already have the elements loaded, if not, the solution I’m going to give you won’t work completely:

&lt;!doctype html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset=&quot;utf-8&quot;&gt;
&lt;title&gt;Filter Testing&lt;/title&gt;
&lt;style&gt;
.container {
padding: 16px;
}
.items-container {
flex-direction: row;
display: flex;
gap: 16px;
}
.item {
flex: 0 0 auto;
width: 25%;
background-color: lightblue;
height: 320px;
}
.hidden-item {
display: &#39;none&#39;;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div class=&quot;container&quot;&gt;
&lt;div class=&quot;items-container&quot;&gt;
&lt;div class=&quot;item&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;item&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;item&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;item&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;hidden-item&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;hidden-item&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;hidden-item&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;hidden-item&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;hidden-item&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;hidden-item&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;hidden-item&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;hidden-item&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;hidden-item&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;hidden-item&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;hidden-item&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;hidden-item&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Showing 4 of 10&lt;/p&gt;
&lt;button id=&quot;loadMoreButton&quot;&gt;
Load more
&lt;/button&gt;
&lt;/div&gt;
&lt;script&gt;
//Let save the button reference 
let loadMoreButton = document.getElementById(&#39;loadMoreButton&#39;);
// Save the hidden items
//Create an array to iterate, because the querySelector return a node list
function loadMoreItems() {
let hiddenItems = document.querySelectorAll(&#39;.hidden-item&#39;);
let displayedItems = document.querySelectorAll(&#39;.item&#39;);
Array.from(hiddenItems).forEach(function (item, index) {
//Lets load just 4 more items
//Remember the array init count in 0, so 3 is the 4 element loaded
if (index &lt; 4) {
//Lets remove the class to hide the element and add the class tho dsiplayed elements
item.classList.remove(&#39;hidden-item&#39;);
item.classList.add(&#39;item&#39;);
}
});
}
// Add the loadMoreItems to button
loadMoreButton.addEventListener(&#39;click&#39;, loadMoreItems);
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

huangapple
  • 本文由 发表于 2023年5月17日 23:42:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76273900.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定