简体   繁体   English

组件按钮先显示n个div元素,然后再显示n个其他元素

[英]Component button to show 'n' number of div elements initially, then show 'n' number more elements

I'm trying to find a component class/button that will show 'n' number of div elements initially, then will show 'n' number more elements each time the button is clicked until there are no more div elements to be displayed. 我试图找到一个组件类/按钮,该组件类/按钮最初将显示'n'个div元素,然后每次单击该按钮将显示'n'个更多的元素,直到不再显示div元素为止。

I am using ASP.NET MVC , so I have a list of elements coming in to my view and being displayed through a for loop. 我正在使用ASP.NET MVC ,因此我有一个进入我的视图并通过for循环显示的元素列表。 I have used a jQuery slider component for something else before to display n elements in a row with the given functionality of left/right drag-scroll functionality and click arrows. 我已经使用jQuery滑块组件进行其他操作,才能在具有向左/向右拖动滚动功能和单击箭头的给定功能的情况下连续显示n个元素。 Will I be able to find a jQuery component or will I have to write my own Javascript? 我将能够找到jQuery组件还是必须编写自己的Javascript?

I have attempted with the following: 我尝试了以下操作:

This is my HTML: 这是我的HTML:

<div class="row whiteBG">
        @foreach (var item in Model.Products)
        {
            <div id="special-products" class="col-sm-3 align-centre">

                @Html.Image(item.ByteImage, item.Name, "128")
                <div class="blend-box category-head" style="background: #323232 url(@Html.ImageSrc(item.ByteImage)) no-repeat 50% 100%;">
                    <div class="item-container">

                        <div class="desc-plus-products">
                            <p>@item.Name</p>
                            <p>@item.Price</p>
                        </div>
                    </div>
                </div>
            </div>
        }
    </div>

<div class="row whiteBG">
    <div class="see-all-image" onclick="SeeMoreProducts()">
        <img src="~/Content/Images/seeAll.jpg" alt="See all items :)" />
    </div>
</div>

I am trying to add click functionality to my image div and have included an onclick"SeeMoreProducts()" function . 我试图将点击功能添加到我的图片div中,并包括一个onclick"SeeMoreProducts()" function

This is my JS: 这是我的JS:

<script>
    $(document).ready(function SeeMoreProducts() {
        $("#special-products:lt(5)").show();
    });
</script>

Use .slice to achieve this and use .on event binding rather than onClick in html 使用.slice实现此目的,并使用.on事件绑定,而不是html中的onClick

var showItem = 5;  // Your desire number of item to show each time
var counter = 1;
$(function() {
  var $box = $('.blend-box');
  $box.slice(showItem).hide();
  $('.see-all-image').on('click', function() {
    $box.slice(0, showItem * counter + showItem).show();
    counter++;
  })
});

 var showItem = 5; // Your desire number of item to show each time var counter = 1; $(function() { var $box = $('.blend-box'); $box.slice(showItem).hide(); $('.see-all-image').on('click', function() { console.log(showItem * counter + showItem); $box.slice(0, showItem * counter + showItem).show(); counter++; }) }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="row whiteBG"> <div id="special-products" class="col-sm-3 align-centre"> <div class="blend-box category-head" style="background: #323232 url(@Html.ImageSrc(item.ByteImage)) no-repeat 50% 100%;"> <div class="item-container"> <div class="desc-plus-products"> <p>@item.Name</p> <p>@item.Price</p> </div> </div> </div> <div class="blend-box category-head" style="background: #323232 url(@Html.ImageSrc(item.ByteImage)) no-repeat 50% 100%;"> <div class="item-container"> <div class="desc-plus-products"> <p>@item.Name</p> <p>@item.Price</p> </div> </div> </div> <div class="blend-box category-head" style="background: #323232 url(@Html.ImageSrc(item.ByteImage)) no-repeat 50% 100%;"> <div class="item-container"> <div class="desc-plus-products"> <p>@item.Name</p> <p>@item.Price</p> </div> </div> </div> <div class="blend-box category-head" style="background: #323232 url(@Html.ImageSrc(item.ByteImage)) no-repeat 50% 100%;"> <div class="item-container"> <div class="desc-plus-products"> <p>@item.Name</p> <p>@item.Price</p> </div> </div> </div> <div class="blend-box category-head" style="background: #323232 url(@Html.ImageSrc(item.ByteImage)) no-repeat 50% 100%;"> <div class="item-container"> <div class="desc-plus-products"> <p>@item.Name</p> <p>@item.Price</p> </div> </div> </div> <div class="blend-box category-head" style="background: #323232 url(@Html.ImageSrc(item.ByteImage)) no-repeat 50% 100%;"> <div class="item-container"> <div class="desc-plus-products"> <p>@item.Name</p> <p>@item.Price</p> </div> </div> </div> <div class="blend-box category-head" style="background: #323232 url(@Html.ImageSrc(item.ByteImage)) no-repeat 50% 100%;"> <div class="item-container"> <div class="desc-plus-products"> <p>@item.Name</p> <p>@item.Price</p> </div> </div> </div> <div class="blend-box category-head" style="background: #323232 url(@Html.ImageSrc(item.ByteImage)) no-repeat 50% 100%;"> <div class="item-container"> <div class="desc-plus-products"> <p>@item.Name</p> <p>@item.Price</p> </div> </div> </div> <div class="blend-box category-head" style="background: #323232 url(@Html.ImageSrc(item.ByteImage)) no-repeat 50% 100%;"> <div class="item-container"> <div class="desc-plus-products"> <p>@item.Name</p> <p>@item.Price</p> </div> </div> </div> <div class="blend-box category-head" style="background: #323232 url(@Html.ImageSrc(item.ByteImage)) no-repeat 50% 100%;"> <div class="item-container"> <div class="desc-plus-products"> <p>@item.Name</p> <p>@item.Price</p> </div> </div> </div> <div class="blend-box category-head" style="background: #323232 url(@Html.ImageSrc(item.ByteImage)) no-repeat 50% 100%;"> <div class="item-container"> <div class="desc-plus-products"> <p>@item.Name</p> <p>@item.Price</p> </div> </div> </div> <div class="blend-box category-head" style="background: #323232 url(@Html.ImageSrc(item.ByteImage)) no-repeat 50% 100%;"> <div class="item-container"> <div class="desc-plus-products"> <p>@item.Name</p> <p>@item.Price</p> </div> </div> </div> <div class="blend-box category-head" style="background: #323232 url(@Html.ImageSrc(item.ByteImage)) no-repeat 50% 100%;"> <div class="item-container"> <div class="desc-plus-products"> <p>@item.Name</p> <p>@item.Price</p> </div> </div> </div> <div class="blend-box category-head" style="background: #323232 url(@Html.ImageSrc(item.ByteImage)) no-repeat 50% 100%;"> <div class="item-container"> <div class="desc-plus-products"> <p>@item.Name</p> <p>@item.Price</p> </div> </div> </div> <div class="blend-box category-head" style="background: #323232 url(@Html.ImageSrc(item.ByteImage)) no-repeat 50% 100%;"> <div class="item-container"> <div class="desc-plus-products"> <p>@item.Name</p> <p>@item.Price</p> </div> </div> </div> <div class="blend-box category-head" style="background: #323232 url(@Html.ImageSrc(item.ByteImage)) no-repeat 50% 100%;"> <div class="item-container"> <div class="desc-plus-products"> <p>@item.Name</p> <p>@item.Price</p> </div> </div> </div> </div> </div> <div class="row whiteBG"> <div class="see-all-image" onclick=""> <img src="~/Content/Images/seeAll.jpg" alt="See all items :)" /> </div> </div> I am trying to add click functionality to my image div and have included an onclick"SeeMoreProducts()" function. This is my JS: 

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

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