js+css 父div,里面有很多子div,当子div在父div中放不下时候,自动滚动子div,向左横向滚动,循环展示子div信息(方法2)

<div
                    id="parentInUserDiv"
                    class="div-in-image flex flex-nowrap"
                  >
                    <div
                      id="childInUserContainer"
                      class="div-in-image-list"
                      ref="srcollInImage"
                    >
                      <template
                        v-for="(item, index) in inUserableTabs"
                        :key="index"
                      >
                        <div
                          class="div-in-image-item flex flex_c justify-around items-center"
                        >
                          <img
                            class="user-img"
                            alt=""
                            src="@/assets/img/home/default-user.png"
                          />
                          <label class="user-label">{{ item.name }}</label>
                          <label class="time-label">{{ item.time }}</label>
                        </div>
                      </template>
                    </div>
                    <div
                      v-show="isShowUl2"
                      id="childInUserContainer2"
                      class="div-in-image-list"
                    ></div>
                  </div>
----------------------------------------------------
.div-in-image {
            width: calc(100% - 36px);
            height: 100%;
            // width: 420px;
            // height: 100px;
            overflow: hidden;
            // overflow-x: auto;
            // position: absolute;
            // left: 3%;
            // right: 3%;

            .div-in-image-list {
              display: flex;
              flex-wrap: nowrap;
              //   margin-left: 5px;
              // margin-right: 5px;
              // width: 100%;

              .div-in-image-item {
                width: 65px;
                margin-left: 5px;
                border: 1px solid #026FDB;
                border-radius: 4px;
                padding-top: 2px;
                padding-bottom: 2px;
                box-sizing: border-box;
              }


              .user-img {
                width: 62px;
                height: 60px;
                border-radius: 4px;
              }

              .user-label {
                font-size: 12px;
                font-weight: bold;
                text-align: center;
              }

              .time-label {
                font-size: 12px;
                color: #359BFF;
                font-weight: 400;
                text-align: center;
              }
            }
          }

          ::-webkit-scrollbar {
            width: 0 !important;
            height: 0 !important;
          }
-----------------------------------------------------------------

const calcInUserWH2 = () => {
  nextTick(() => {
    const ul1 = document.getElementById("childInUserContainer");
    let ul2 = document.getElementById("childInUserContainer2");
    if (ul1 && ul2) {
      // 根据内容是否超过容器高度来决定是否显示第二个ul
      ul2.innerHTML = ul1.innerHTML;
    }
    // const rollSet = optionsSetUp.value;
    const rollSet = {
      isRoll: true,
      scroll: 1,
      interTime: 1000,
    };
    const dom = document.getElementById("parentInUserDiv"); //获取的table元素
    console.log(dom);
    // 是否开启滚动 table是否出现滚动条
    if (rollSet["isRoll"] && dom && dom["scrollWidth"] > dom["clientWidth"]) {
      // 开始无滚动时设为0
      dom["scrollLeft"] = 0;
      isShowUl2.value = true;

      timerInUserSroll.value = setInterval(() => {
        dom["scrollLeft"] = dom["scrollLeft"] + rollSet["scroll"] * 70; // 滚动个数

        // //判断是否滚动到底部
        if (ul1 && dom["scrollLeft"] >= ul1["scrollWidth"]) {
          dom["scrollLeft"] = 0; //置为0  继续滚动
        }
      }, rollSet["interTime"]);
    } else {
      isShowUl2.value = false;
      if (dom) {
        dom["scrollLeft"] = 0; // 开始无滚动时设为0
      }
    }
  });
};
posted @ 2024-02-02 14:10  小小菜鸟04  阅读(13)  评论(0)    收藏  举报