mask遮罩配合<input text=“range“>实现图片切换

效果图

mask

实现代码

 //这里使用的是react
 function MaskPicture{
 
   //存储input 的range值并设置初始值为50 放在中间
   const [range,setRange] = useState(50)

   //改变range值
   const handleChangeInput =(e: any) => {
	    console.log(e.target.value);
	    setRange(e.target.value)
    
  }

	const picture1 = {
	    backgroundImage: "url(https://static.cn/image/1714ffd693d4d0df3c315fd2b7f4.png)",
	    //确保浏览器兼容性
	    WebkitMaskImage: `linear-gradient(to right, #000 0, ${range}%, #0000 0)`,
	    maskImage:`linear-gradient(to right, #000 0, ${range}%, #0000 0)`
  }
  	const picture2 = {
	    backgroundImage: "url(https://static.cn/image/3ac2bc16b25e9a62a9896ac39aa.png)",
	    WebkitMaskImage: `linear-gradient(to left, #000 0, ${100-range}%, #0000 0)`,
	    maskImage:`linear-gradient(to left, #000 0, ${100-range}%, #0000 0)`
  }
	return (
	    <div className={styles.img}>
          <div className={styles.contrast_picture1} style={picture1} />
          <div className={styles.contrast_picture2} style={picture2}/>
          <input type="range" id="range" step="0.1" onChange={(e)=>handleChangeInput(e)}/>
        </div>
	)
}
// css 
.img {
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    height: 86vh;
    div{
      position: absolute;
      top: 0;
      width: 61vw;
      height: 82vh;
      background-repeat: no-repeat;
      background-size: cover;
    }
    input[type="range"] {
      z-index: 1;
      position: absolute;
      top:0;
      appearance: none;
      width: 59vw;
      height: 10vh;
      background: transparent;
      cursor: pointer;
      -webkit-appearance: none;
    }
    input[type=range]:focus{
      outline:none;
  }
    //定制滑块样式
    input[type="range"]::-webkit-slider-thumb {
      z-index: 1;
      appearance: none;
      -webkit-appearance: none;
      width: 20px;
      height: 20px;
      border-top: 10px solid #352058; 
      border-left: 10px solid #352058; 
      border-bottom: 10px solid #b3b3b3;
      border-right: 10px solid #b3b3b3;
      transform: rotate(135deg);
    } 
    
    //兼容浏览器
    input[type="range"]::-moz-range-thumb {
      z-index: 1;
      appearance: none;
      -webkit-appearance: none;
      width: 20px;
      height: 20px;
      border-top: 10px solid #352058; 
      border-left: 10px solid #352058; 
      border-bottom: 10px solid #b3b3b3;
      border-right: 10px solid #b3b3b3;
      transform: rotate(45deg);
    }
    //定制滑动条样式
    input[type=range]::-webkit-slider-runnable-track{
      width: 55vw;  
      height: 15px;
  }
      input[type=range]::-moz-range-track{
      width: 55vw;  
      height: 15px;
  }

  }
  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
给下面代码补充一个样式:<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>计时器</title> <link rel="preload" href="fonts/mui.ttf" as="font" crossorigin> <link rel="stylesheet" href="vendor/mui.min.css"> <link rel="stylesheet" href="vendor/waves.min.css"> <link rel="stylesheet" href="static/general-48e00d0585.css"> <link rel="shortcut icon" href="https:assets.retiehe.com/rth-legacy-icom-512.png"> <script src="static/theme-d5fe8b3fc6.js"></script> <meta name="theme-color" content="#ffffff"> </head> <body class="timer"> <div id="bg-img" class="bg-img"></div> <main> <div id="time" data-v-app> <div class="mui-input-row"> <input class="display" type="text"> </div> <div class="mui-input-row mui-input-range"> <label for="hour">时</label> <input id="minute" tpye="range" min="0" max="59"> </div> <div class="mui-input-row mui-input-rang"> <label for="second">秒</label> <input id="second" type="range" min="0" max="59"> </div> </div> <select> <option value="Countdown">倒计时</option> <option value="Stopwatch">秒表</option> <option value="CurrentTime">当前时间</option> </select> <button id="star-btn" type="button" class="mui-bin mui-btn-blue mui-btn-block waves-effect wave-light">开始</button> <button id="reset-btn" tpye="button" class="mui-btn mui-btn-block waves-effect waves-light">复位</button> </main> <script scr="static/i18n-36955081a6.js"></script> <script scr="static/general-e70b71a82f.js"></script> <script scr="https://static.retiehe.com/js/waves.min.js"></script> <script scr="static/timer-181c3930f6.js"></script> <script async scr="https://static.retiehe.com/js/wave.min.js"></script> <header class="mui-bar mui-bar-nav"> <div id="back-btn" class="mui-icon mui-icon-contact mui-pull-right" tabindex="0" role="button" aria-label="用户"></div> </header> <div class="mask" tabindex="0" aria-label="关闭弹窗" hidden></div> </body> </html>
05-31
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值