js+cavansはピクチャースライダの検証を実現します。
ここでは、js+cavansの画像ブロック検証を実現するための具体的なコードを共有します。
jsはすでにカプセル化されています。持ってきて使えば、pc端と移動端の互換性があります。
効果:
移動先:
pc端:
原理は説明しません。前のブログでも話しましたが、このバージョンはcanvasと結合して実現しただけです。またpcと互換しています。直接コードを持って使えます。
コード:html
jsはすでにカプセル化されています。持ってきて使えば、pc端と移動端の互換性があります。
効果:
移動先:
pc端:
原理は説明しません。前のブログでも話しましたが、このバージョンはcanvasと結合して実現しただけです。またpcと互換しています。直接コードを持って使えます。
コード:html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.sliderModel {
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
display: flex;
justify-content: center;
align-items: center;
display: none;
}
.sliderModel .cont{padding: 20px; width: 280px; background: #fff;border-radius: 6px;margin: 50px auto;}
.showMessage {
text-align: center;
font-size: 14px;
height: 30px;
line-height: 30px;
}
#canvas_wrap{
width: 280px;
}
#canvas_wrap canvas{
display: block;
}
</style>
</head>
<body>
<div class="sliderModel">
<div class="cont">
<div id="canvas_wrap"></div>
<div class="showMessage"></div>
</div>
<div id="close"> </div>
</div>
<div id="open"> </div>
</body>
<script src="newSlider.js"></script>
<script>
function showMessage(msg,color){
var showMessage = document.querySelector('.showMessage');
showMessage.innerHTML=msg;
showMessage.style.color=color;
}
var obj={
el:'#canvas_wrap',
w:280,
h:150,
imgArr:['images/sliderz/1.jpg','images/sliderz/2.jpg','images/sliderz/3.jpg','images/sliderz/4.jpg'],
sliderIcon:'images/sliderz/sangangy.png',
refresh:function(){
showMessage('','#333')
},
finish:function(e){
e? showMessage(' !','green') : showMessage(' , ','red');
}
}
document.querySelector('#open').onclick=function(){ //
document.querySelector('.sliderModel').style.display="block";
new window.$newSlider(obj);
}
document.querySelector('#close').onclick=function(){ //
document.querySelector('.sliderModel').style.display="none";
}
</script>
</html>
newSlider.js:
(function(){
function slider(params){
var obj={
el:params.el,
w:params.w || 280, //canvas
h:params.h || 150, //canvas
range:params.range || 5, //
imgArr:params.imgArr || [], //
sliderW:36, //slider
sliderIcon:params.sliderIcon || '',
refresh:params.refresh, //
finish:params.finish , //
};
// canvas
var container=document.querySelector(obj.el);
container.innerHTML='';
var canvas_wrap=document.createElement('div');
canvas_wrap.className="canvas_wrap";
canvas_wrap.style.cssText="position:relative;overflow:hidden;border-radius:4px;width:"+obj.w+"px;height:"+obj.h+"px;background:#fff"
// canvas
var bigCanvas=document.createElement('canvas');
var smartCanvas=bigCanvas.cloneNode(true);
bigCanvas.width=smartCanvas.width=obj.w;
bigCanvas.height=smartCanvas.height=obj.h;
bigCanvas.style.cssText=smartCanvas.style.cssText="position:absolute;left:0;top:0";
var bcxt=bigCanvas.getContext('2d'),scxt=smartCanvas.getContext('2d'),img=new Image();
//
var titleDom=document.createElement('div');
var refreshDom=document.createElement('div');
titleDom.className="slider_title";
refreshDom.className="slider_refresh";
titleDom.style.cssText="position:relative;width:"+obj.w+"+px;height:60px;text-align:center;font-size:18px; line-height:60px";
refreshDom.style.cssText="position:absolute;top:0;right:14px;font-size:14px;color:green;cursor: pointer";
titleDom.innerHTML=" ";
refreshDom.innerHTML=" ";
//
var slider_wrap=document.createElement('div'),slider=document.createElement('div'),sliderCover=document.createElement('div');
slider_wrap.className="slider_wrap";
slider.className="canvas_slider";
sliderCover.className="sliderCover";
slider_wrap.innerText=" ";
slider_wrap.style.cssText="width:"+obj.w+"px;height:30px; border-radius:30px;line-height:30px; position:relative;margin-top:10px;text-align:center;box-shadow: inset 0 0 4px #ccc;font-size: 14px;color:#999";
slider.style.cssText="cursor: pointer;position: absolute;left: 0;top: 50%;z-index: 2;height: "+obj.sliderW+"px;width: "+obj.sliderW+"px;background:rgb(0, 124, 255) url("+obj.sliderIcon+") no-repeat center;background-size: 60% 60%;border-radius: "+obj.sliderW+"px;line-height:"+obj.sliderW+"px;text-align:center;transform: translateY(-50%);";
sliderCover.style.cssText="position: absolute;left: 0;top:0;width:0;height:100%;background:#eee;border-radius:30px;"
slider_wrap.appendChild(slider);
slider_wrap.appendChild(sliderCover);
canvas_wrap.appendChild(bigCanvas);
canvas_wrap.appendChild(smartCanvas);
titleDom.appendChild(refreshDom);
container.appendChild(titleDom);
container.appendChild(canvas_wrap);
container.appendChild(slider_wrap);
var canvasCoverL=0,startDownX=0,smartCanvasBL=0,sliderMaxRange=obj.w-obj.sliderW;
/*
canvasCoverL: canvas x
startDownX:// x
smartCanvasBL: canvas left
sliderMaxRange:slider
*/
// canvas
function creatCanvas(){
//
canvasCoverL=0;startDownX=0;smartCanvasBL=0;
slider.style.left = sliderCover.style.width = 0;
var l= 40, // ,
r = 10, //
PI = Math.PI,
sliderW=l+2*r, //
rand=canvasSize(sliderW,r), // x,y,left
x = canvasCoverL= rand.x, // x
y = rand.y; // y
smartCanvasBL=rand.left;
//
bcxt.clearRect(0, 0, obj.w, obj.h)
scxt.clearRect(0, 0, obj.w, obj.h)
smartCanvas.width=obj.w;
var srcIndex=Math.floor(Math.random()*(obj.imgArr.length-1));
img.src=obj.imgArr[srcIndex];
draw(scxt,x,y,l,r,PI,'clip');
draw(bcxt,x,y,l,r,PI,'fill');
img.onload = function() { // onload , canvas
bcxt.drawImage(img,0,0,obj.w,obj.h);
scxt.drawImage(img,0,0,obj.w,obj.h);
//
var ImageData = scxt.getImageData(x, y-2*r, sliderW, sliderW)
smartCanvas.width = sliderW;
smartCanvas.style.left=rand.left+"px";
scxt.putImageData(ImageData, 0, y-2*r)
}
obj.refresh && obj.refresh();
}
// canvas ,
function canvasSize(cw,r){
// cw ,r
var random =Math.random();
var x=Math.floor(obj.w/2 + random*(obj.w/2 - cw)); //x x ,
var y=Math.floor(r*2+random*(obj.h - cw - r*2)); //y y ,( 2 , )
var left =Math.floor(random*(obj.w/2 - cw)); //canvas left ,
return {x:x,y:y,left:left}
}
// canvas
function draw(ctx,x,y,l,r,PI,operation) {
ctx.beginPath()
ctx.moveTo(x, y)
ctx.arc(x + l / 2, y - r + 2, r, 0.72 * PI, 2.26 * PI)
ctx.lineTo(x + l, y)
ctx.arc(x + l + r - 2, y + l / 2, r, 1.21 * PI, 2.78 * PI)
ctx.lineTo(x + l, y + l)
ctx.lineTo(x, y + l)
ctx.arc(x + r - 2, y + l / 2, r + 0.4, 2.76 * PI, 1.24 * PI, true)
ctx.lineTo(x, y)
ctx.lineWidth = 1
ctx.fillStyle = 'rgba(200, 200, 200, 1)'
ctx.strokeStyle = 'rgba(255, 255, 255, 0.7)'
ctx.stroke()
ctx[operation]()
ctx.globalCompositeOperation = 'destination-over'
}
//
function moveStart(e){
var ev = e || window.event;
startDownX = ev.touches!=undefined? ev.touches[0].clientX : ev.clientX;
}
//
function moveProcess(e){
var ev = e || window.event,downX = (ev.touches!=undefined)? ev.touches[0].clientX : (startDownX!=0? ev.clientX : 0),range=downX-startDownX;
console.log(downX)
var sliderRange= range<=0? 0 : (range<sliderMaxRange ? range : sliderMaxRange);
slider.style.left=sliderRange+"px";
sliderCover.style.width=obj.sliderW/2 + sliderRange +"px";
smartCanvas.style.left=smartCanvasBL+sliderRange+"px";
}
//
function moveEnd(e){
var ev = e || window.event;
ev.touches!=undefined? slider.ontouchmove=null : slider.onmousemove=null;
var smartCanvasL= parseInt(smartCanvas.style.left);
if(Math.abs(canvasCoverL - smartCanvasL) < obj.range){
obj.finish && obj.finish(true);
}else{
obj.finish && obj.finish(false);
var timer = null,step = 10;
var sliderL = parseInt(slider.style.left)
timer = setInterval(function () {
sliderL -= step;
step += 5;
if (sliderL <= 0) {
clearInterval(timer);
sliderL = 0;
slider.style.left = sliderCover.style.width = 0;
smartCanvas.style.left = smartCanvasBL + "px"
}
slider.style.left = sliderL + "px";
sliderCover.style.width = sliderL+obj.sliderW/2 +"px";
smartCanvas.style.left = sliderL + smartCanvasBL+ "px";
}, 20)
}
}
//
creatCanvas();
refreshDom.onclick=refreshDom.ontouchstart=creatCanvas;
slider.ontouchstart=function(){
moveStart();
this.ontouchmove=moveProcess;
this.ontouchend=moveEnd;
};
slider.onmousedown=function(){
moveStart();
this.onmousemove=moveProcess;
this.onmouseup=moveEnd;
};
}
window.$newSlider=slider
})()
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。