JAVASCRIPT
VUE
REACT
NODE
ES6
TYPESCRIPT

页面竖向滚动和横向滚动

2023. 04. 25    

页面竖向滚动和横向滚动

页面竖向滚动和横向滚动,手动横向滚动fn

结果

<template>
    <div class="bargaining-wrap">
        <div class="country-list">
            <span v-for="(item,i) in countryList" :key="i" :class="{'col':item==currentCountry}" @click="selCountry(item)"></span>
        </div>
        <template>
			<div class="margin-top-20" :style="isScroll?'width: 100%;position: fixed;background: #fff;z-index: 8;top:40px;border-top: 1px solid #E8E8E8':'border-top: 1px solid #E8E8E8;'">
				<div class="form-item border flex-h" id="fixed-box">
					<div class="form-label">
						<div class="label flex-v-c"></div>
					</div>
					<div class="form-content padding-0 flex-a-i" style="overflow: hidden;user-select: none;">
						<div class="scroll-wrap">
							<div class="col-item w165" v-for="(item, index) in 20" :key="index">
								<div class="thumb-wrap">
									<img src="//up.enterdesk.com/photo/2009-9-21/enterdesk.com-0E18CB4404AE89192005183F394F1EAB.JPG" class="thumb">
								</div>
								<div class="status-wrap">
									<!-- <div class="status status-green" v-if="item.avlStatus == 2">Passed</div>
									<div class="status status-yellow" v-if="item.avlStatus == 1">Progress</div> -->
									<div class="status status-yellow">Progress</div>
								</div>
								<div class="btn-delete" @click="handleDelete()"></div>
								<div class="text-wrap" style="text-align: center;margin-top: 10px;">
									<div class="tips" style="white-space: normal;">阿三倒萨大大</div>
								</div>
							</div>
						</div>
					</div>
				</div>
				<div class="form-item border flex-h">
					<div class="scroll-bar-wrap" id="scroll-bar-wrap">
						<div class="scroll-bar" id="scroll-bar"></div>
					</div>
				</div>
			</div>
            <div v-if="isScroll" :style="'margin-top:20px;width:100%;height:'+fixedBoxH+'px'"></div>
        </template>
        <div>
            内容
        </div>
        <div class="margin-top-20 border-top">
            <div class="form-item border  flex-h margin-top-20 border-top">
                <div class='form-item-title bold'>Extra Cost / (Saving) - Production Other Options</div>
            </div>
            <template v-for="(item,j) in 5">
                <div class="form-item border  flex-h " :key="'QQ_'+j">
                    <div class='form-item-title'>阿萨大大撒</div>
                </div>
                <div class="form-item border  flex-h " :key="'BB_'+j">
                    <div class="form-label">
                        <div class="label  flex-v-c">
                            <div class="tips bold" style="white-space: normal;">Unit Price <br>(Each Kit)</div>
                        </div>
                    </div>
                    <div class="form-content  padding-0  flex-v-c">
                        <div class="scroll-wrap">
                            <div class="col-item w165" v-for="(sitem,i) in 20" :key="'Bp_'+i+'s'+j">
                                <div class="flex-v-c" style="height: 100%;">
                                    <div class="tips" >$121.15</div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </template>
        </div>
		
		<div>
            内容
        </div>
        <div class="margin-top-20 border-top">
            <div class="form-item border flex-h margin-top-20 border-top">
                <div class='form-item-title bold'>Extra Cost / (Saving) - Production Other Options</div>
            </div>
            <template v-for="(item,j) in 5">
                <div class="form-item border flex-h " :key="'QQ_'+j">
                    <div class='form-item-title'>阿萨大大撒</div>
                </div>
                <div class="form-item border flex-h " :key="'BB_'+j">
                    <div class="form-label">
                        <div class="label flex-v-c">
                            <div class="tips bold" style="white-space: normal;">Unit Price <br>(Each Kit)</div>
                        </div>
                    </div>
                    <div class="form-content  padding-0  flex-v-c">
                        <div class="scroll-wrap">
                            <div class="col-item w165" v-for="(sitem,i) in 20" :key="'Bp_'+i+'s'+j">
                                <div class="flex-v-c" style="height: 100%;">
                                    <div class="tips" >$121.15</div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </template>
        </div>
		

    </div>
</template>
<script>
export default {
  data() {
    return {
        fixedBoxH:0,
        isScroll:false,
        currentCountry: "All",
        countryList: ["All", "China", "USA", "Mexico", "India", "Offline"],
    };
  },
  mounted(){
    document.addEventListener('scroll', this.handleScroll, true);
    this.$nextTick(()=>{
        this.fixedBoxH = document.getElementById('fixed-box')&&document.getElementById('fixed-box').clientHeight||0;
    })
	this.checkScroll();
  },
  methods: {
    selCountry(val) {
      this.currentCountry = val;
    },
    handleScroll(e){
      	let bargainingWrap = document.querySelector('.bargaining-wrap');
		let domOffsetTop = bargainingWrap.offsetTop;
        if(e.target.scrollTop>domOffsetTop || (e.target.scrollTop == undefined && this.isScroll == true)){
            this.isScroll = true;
        }else{
            this.isScroll = false;
        }
    },
	setScrollLeft: function(dur) {
		let scrollObjs = document.getElementsByClassName('scroll-wrap');
		
		for(let i = 0; i < scrollObjs.length; i++) {
			scrollObjs[i].style.left = dur + 'px';
		}
	},
	checkScroll: function() {
		this.$nextTick(() => {
			let scrollBarWrap = document.getElementById('scroll-bar-wrap'),
				scrollBar = document.getElementById('scroll-bar'),
				scrollObjs = document.getElementsByClassName('scroll-wrap');

			if(!scrollBarWrap || !scrollBar || !scrollObjs) {
				return false;
			}

			let _width = scrollBarWrap.scrollWidth, // 滚动条容器宽度
				_wholeWidth = scrollObjs[0].scrollWidth; // 滚动内容的宽度

				// console.log(_width, _wholeWidth);
			
			if(_wholeWidth > _width) {
				// 需要显示滚动条
				scrollBarWrap.style.opacity = 1;
				this.setScrollLeft(0);
				scrollBar.style.left = '0';
				// scrollBar.style.width = parseInt(_width * _width / _wholeWidth) + 'px';
				scrollBar.style.width = (_width * _width / _wholeWidth) + 'px';

				let _barWidth = scrollBar.scrollWidth;
				let _bl = (_wholeWidth - _width) / (_width - _barWidth);

				scrollBar.onmousedown = null;
				scrollBar.onmousedown = (e) => {
					// if(e.target.id != 'scroll-bar') {
					//     return false;
					// }

					let x_start = e.screenX,
						barLeft = parseFloat(scrollBar.style.left) || 0,
						objLeft = parseFloat(scrollObjs[0].style.left) || 0;

					document.onmousemove = null;
					document.onmousemove = (me) => {
						let x_end = me.screenX,
							x_duration = x_end - x_start;

						if(!((barLeft + x_duration) < 0 || (barLeft + x_duration + _barWidth) > _width)) {
							scrollBar.style.left = (barLeft + x_duration) + 'px';
							this.setScrollLeft(parseFloat(objLeft - x_duration * _bl));
						}

						if(barLeft + x_duration < 0) {
							scrollBar.style.left = '0px';
							this.setScrollLeft(0);
						} else if(barLeft + x_duration + _barWidth > _width) {
							scrollBar.style.left = (_width - _barWidth) + 'px';
							this.setScrollLeft(_width - _wholeWidth);
						} else {
							scrollBar.style.left = (barLeft + x_duration) + 'px';
							this.setScrollLeft(parseFloat(objLeft - x_duration * _bl));
						}
					}

					document.onmouseup = null;
					document.onmouseup = () => {
						document.onmousemove = null;
					};
				};
			} else {
				// 隐藏滚动条
				scrollBarWrap.style.opacity = 0;
				this.setScrollLeft(0);
			}
		});
	},
  },
};
</script>
<style lang='scss' scoped>
@import "../../../../common/styles/base/mixin-common";
.bargaining-wrap {
  margin: 20px;
  overflow: hidden;
}
.country-list {
  display: flex;
  align-items: center;
  span {
    user-select: none;
    display: inline-block;
    color: #363636;
    background: #f3f3f3;
    font-size: 12px;
    height: 36px;
    padding: 0 20px;
    margin-right: 20px;
    border-radius: 5px;
    border: 1px solid #f3f3f3;
    line-height: 34px;
    cursor: pointer;
    &.col {
      color: #5d48ff;
      background: #f5f3ff;
      border: 1px solid #5d48ff;
    }
  }
}
.form-item {
  position: relative;
  .form-item-title {
    padding: 10px 5px 10px 10px;
    min-height: 30px;
    font-size: 12px;
    color: rgba(0, 0, 0, 0.85);
    line-height: 30px;
    &.bold {
      font-weight: $--fw-bold;
    }
  }

  .form-label {
    position: relative;
    width: 135px;
	flex: 135px 0 0;
	z-index: 2;
	background: #fff;
    .label {
      padding: 10px 5px 10px 10px;
      min-height: 60px;
      font-size: 12px;
      color: rgba(0, 0, 0, 0.85);
      line-height: 20px;

      &.bold {
        font-weight: $--fw-bold;
      }

      .bold-none {
        font-weight: normal;
      }
    }

    .icon-arrow {
      @include position(absolute, 1, 50%, 15px, none, none);
      @include translate(0, -50%, 0);
      font-size: 14px;
      cursor: pointer;

      &:hover {
        opacity: 0.7;
      }
      &:active {
        opacity: 0.85;
      }
    }
  }

  .form-content {
    position: relative;
    padding: 10px 0;
    font-size: 0;
    // overflow: hidden;

    &.padding-0 {
      padding: 0 !important;
    }
  }
  .scroll-wrap {
    position: relative;
    height: 100%;
    vertical-align: middle;
    font-size: 0;
    white-space: nowrap;

    .flex-wrap {
      height: 100%;
    }
  }
  &.border{
    border-bottom: 1px solid #E8E8E8;

    .form-label{
        .label{
            min-height: 60px;
        }
    }

    .form-content{
        position: relative;
        padding: 10px;
        border-left: 1px solid #E8E8E8;

        &.border-none{
            border-left: 1px solid transparent;

            .col-item{
                border-left: 1px solid transparent;
            }
        }
    }
  }
  .tips{
    display: inline-block;
    vertical-align: middle;
    font-size: 12px;
    line-height: 16px;
    color: rgba(0, 0, 0, .85);

    &.bold{
        font-weight: $--fw-bold;
    }

    &.color-red{
        color: #FF0000;
    }
    &.color-orange{
        color: #FF8E00;
    }
  }

  .col-item {
    position: relative;
    display: inline-block;
    vertical-align: middle;
    padding: 10px;
    height: 100%;
    min-height: 50px;
    border-left: 1px solid #e8e8e8;

    &:first-child {
      border-color: transparent !important;
    }

    &.border {
      border-left: 1px solid #e8e8e8;
    }

    &.error-class {
      border: 1px solid #ff0000 !important;
    }

    &.w100 {
      width: 101px;
      padding-left: 5px;
      padding-right: 5px;

      .ellipsis {
        max-width: 100%;
        height: 20px;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
      }
    }
    &.w165 {
      width: 165px;
    }
    &.w180 {
      width: 181px;
    }
    &.w320 {
      width: 320px;
    }

    .button-web {
      width: 100%;
    }

    .align-center {
      text-align: center;
    }

    .btn-close {
      @include position(absolute, 3, 0, 0, none, none);
      width: 20px;
      height: 20px;
      cursor: pointer;

      &::before {
        content: "";
        display: block;
        width: 8px;
        height: 8px;
        margin: 6px 0 0 6px;
        background-image: url(/static/img/icon_modal.b2e1212f.png);
        background-repeat: no-repeat;
        background-position: -5px -5px;
        background-size: 180px auto;
        /*opacity: .5;*/
      }

      &:hover {
        opacity: 0.7;
      }
      &:active {
        opacity: 0.85;
      }
    }

    .thumb-wrap {
      position: relative;
      width: 60px;
      height: 60px;
      margin: 0 auto;
      border-radius: 50%;
      overflow: hidden;
      background: #c7c7c7;
      @include translate(0, 0, 0);

      .thumb {
        @include position(absolute, 1, 50%, none, none, 50%);
        max-width: 100%;
        max-height: 100%;
        @include translate(-50%, -50%, 0);
      }
    }

    .status-wrap {
      text-align: center;

      .status {
        display: inline-block;
        margin-top: 15px;
        padding: 0 10px;
        font-size: 12px;
        line-height: 20px;
        color: #fff;
        border-radius: 15px;
        white-space: nowrap;
        font-weight: $--fw-bold;

        &.status-green {
          background-color: #4fa11f;
        }

        &.status-yellow {
          background-color: #ecb900;
        }
      }
    }

    .btn-delete {
      @include position(absolute, 1, 0, 0, none, none);
      width: 32px;
      height: 32px;
      overflow: hidden;
      cursor: pointer;

      &::after {
        content: "";
        display: block;
        width: 12px;
        height: 12px;
        margin: 10px 0 0 10px;
        @include backgroundImage(
          "../../../../assets/icon_modal.png",
          no-repeat,
          -10px,
          -10px,
          322px,
          auto
        );
        opacity: 0.5;
      }

      &:hover {
        &::after {
          opacity: 0.3;
        }
      }
      &:active {
        &::after {
          opacity: 0.4;
        }
      }
    }
  }
}
.border-top{
    border-top: 1px solid #E8E8E8;
}
.scroll-bar-wrap{
	@include position(absolute, 400, -10px, 0, none, 135px);
	border-radius: 20px;
	height: 10px;
	background: rgba(0, 0, 0, .05);
	opacity: 0;

	&:hover{
		background: rgba(0, 0, 0, .1);
	}

	.scroll-bar{
		position: relative;
		border-radius: 20px;
		height: 100%;
		background: rgba(0, 0, 0, .15);

		&:hover{
			background: rgba(0, 0, 0, .3);
		}
	}
}
</style>