2017-10-30 30 views
1

私の問題です: 私はこのCSSキューブのスクロールをしようとしてきました。そして私はそれをやった!ここにそれが働いた方法です:https://codepen.io/vaninoo/pen/BmyYQdドラッグ&スクロールでjavascriptキューブを回転

私は非常に満足していました。しかし、友人と一緒にそれをテストした後、彼らの多くは回転させるためにキューブを "ドラッグ"したかったようです。だから私はこの機能を追加することにしました。これは私が得た最も近いものです:https://codepen.io/vaninoo/pen/jaEZBx

キューブはドラッグで回転でき、ドラッグした後にスクロールすることで回転できます。しかし:

  1. 私がスクロールするたびに、前にクリックしなくてもそれを回したいと思います。
  2. 私はそれを移動するときに "ジャンプ"を削除する方法を見つけることができません。キューブの位置を記憶しておく必要がありますが、私は非常に努力してしまいました。ここで

あなたは私のペンで見つけることができるコードは(「最も近い私が得た」)である:

// START OF UNSURE PART 
 

 
$('document').ready(function() { 
 
\t var lastScrollTop = 0; 
 
\t $(window).scroll(function trucenscroll(event) { 
 
\t var st = $(this).scrollTop(); 
 
\t var sl = $(this).scrollLeft(); 
 
\t if (st > lastScrollTop) { 
 
\t   
 
\t  //Le cube tourne 
 
\t \t  var p1,angle,i,tmp; 
 
\t \t \t 
 
\t \t \t p1 \t = {'x': sl - p0.x, \t 'y': st - p0.y }, 
 
\t \t \t angle \t = {'x': -p1.y * unit, \t \t 'y': p1.x * unit}; 
 
\t \t 
 
\t \t \t for(i = 0; i < faces.length; i++) 
 
\t \t \t { 
 
\t \t \t \t tmp = 'rotateX(' + angle.x + 'deg)' + ' rotateY(' + angle.y + 'deg)' + styles[i]; 
 
\t \t \t \t faces[i].style.transform = p + tmp; 
 
\t \t \t \t faces[i].style['-webkit-transform'] = p + tmp; 
 
\t \t \t } 
 
\t } 
 
\t else if(st == lastScrollTop) { 
 
\t  //do nothing 
 
\t  //In IE this is an important condition because there seems to be some instances where the last scrollTop is equal to the new one 
 
\t } 
 
\t else { 
 
\t  \t \t var p1,angle,i,tmp; 
 
\t \t \t p1 \t = {'x': sl - p0.x, \t 'y': st - p0.y }, 
 
\t \t \t angle \t = {'x': -p1.y * unit, \t \t 'y': p1.x * unit}; 
 
\t \t 
 
\t \t \t for(i = 0; i < faces.length; i++) 
 
\t \t \t { 
 
\t \t \t \t tmp = 'rotateX(' + angle.x + 'deg)' + ' rotateY(' + angle.y + 'deg)' + styles[i]; 
 
\t \t \t \t faces[i].style.transform = p + tmp; 
 
\t \t \t \t faces[i].style['-webkit-transform'] = p + tmp; 
 
\t \t \t } 
 
\t } 
 
\t lastScrollTop = st; 
 
\t }); 
 
}); 
 

 

 
// END OF UNSURE PART 
 

 

 

 

 

 

 

 

 
init(); \t \t 
 
//=========================================================== 
 
// \t \t \t onMouseMove 
 
//=========================================================== 
 
function onMouseMove(e) 
 
{ 
 
\t var p1,angle,i,tmp; 
 

 
\t if (! dragging) return; 
 

 
\t p1 \t = {'x': e.clientX - p0.x, \t 'y': e.clientY - p0.y }, 
 
\t angle \t = {'x': -p1.y * unit, \t \t 'y': p1.x * unit}; 
 
    
 
\t for(i = 0; i < faces.length; i++) 
 
\t { 
 
\t \t tmp = 'rotateX(' + angle.x + 'deg)' + ' rotateY(' + angle.y + 'deg)' + styles[i]; 
 
\t \t faces[i].style.transform = p + tmp; 
 
\t \t faces[i].style['-webkit-transform'] = p + tmp; 
 
\t } 
 
} 
 
//=========================================================== 
 
// \t \t \t onMouseDown 
 
//=========================================================== 
 
function onMouseDown(e) 
 
{ 
 
\t var element; 
 

 
\t onMouseUp(); \t // disable if dragging 
 

 
\t element = e.target; 
 
\t //if (! element.classList.contains('face')) return false; 
 

 
\t e.preventDefault(); 
 
\t window.p0 \t = { 'x': e.clientX, 'y': e.clientY }; 
 
\t dragging \t = true; 
 
\t return false; 
 
} 
 
//=========================================================== 
 
// \t \t \t onMouseUp 
 
//=========================================================== 
 
function onMouseUp(e) 
 
{ 
 
\t var i,tmp,style; 
 

 
\t if (! dragging) return; 
 
\t dragging = false; 
 

 
\t for (i = 0; i < faces.length; i++) 
 
\t { 
 
\t \t style = faces[i].style; 
 
\t \t tmp = style.transform || style['-webkit-transform']; 
 
\t \t styles[i] = tmp.replace('perspective(32em) ', ''); 
 
\t } 
 
    
 
} 
 
//===================================================================== 
 
// \t \t \t initializeCube 
 
//===================================================================== 
 
function initializeCube() 
 
{ 
 
\t var i,tmp; 
 

 
\t for (i = 0; i < faces.length; i++) 
 
\t { 
 
\t \t if (i < 4) tmp = 'rotateY(' + i*90 + 'deg)'; 
 
\t \t if (i >= 4) tmp = 'rotateX(' + Math.pow(-1, i) * 90 + 'deg)'; 
 
\t \t tmp += ' translateZ(' + side/2 + 'px)'; 
 
\t 
 
\t \t faces[i].style.transform = p + tmp; 
 
\t \t faces[i].style['-webkit-transform'] = p + tmp; 
 
\t \t styles.push(tmp); 
 
\t } 
 
} 
 
//===================================================================== 
 
// \t \t \t init 
 
//===================================================================== 
 
function init() 
 
{ 
 
\t window.addEventListener('mousedown', onMouseDown, false); 
 
\t window.addEventListener('mouseup', onMouseUp, false); 
 
\t window.addEventListener('mousemove', onMouseMove, false); 
 

 
\t window.faces \t \t = document.querySelectorAll('.face'); 
 
\t window.styles \t \t = new Array(); 
 
\t window.style \t \t = getComputedStyle(faces[0]); 
 
\t window.factor \t \t = 3; 
 
\t window.side \t \t = parseInt(style.width.split('px')[0], 10); 
 
\t window.max_amount \t = factor * side; 
 
\t window.unit \t \t = 360/max_amount; 
 
\t window.dragging \t = false; 
 
\t window.scrolling \t = false; 
 
\t window.p \t \t = 'perspective(32em)'; 
 

 
\t initializeCube(); 
 
}
body { 
 
\t position: relative; 
 
\t height:5000px; 
 
} 
 

 
.cube, .cube * 
 
{ 
 
    position: absolute; 
 
    top: 25vh; 
 
    left: 50%; 
 
} 
 
    
 
.cube 
 
{ 
 
    user-select: none; 
 
    cursor: move; 
 
} 
 
.cube div span 
 
{ 
 
\t position: relative; 
 
\t top: 60px; 
 
\t left: -5px; 
 
\t font-size: 8em; 
 
} 
 

 
.face 
 
{ 
 
\t box-sizing: border-box; 
 
\t border: solid 1px; 
 
\t margin: -8em; 
 
\t width: 16em; 
 
\t height: 16em; 
 
\t box-shadow: inset 0 0 15px rgba(0,0,255,0.6); 
 
\t text-align: center; 
 
\t /** backface-visibility: hidden; /**/ 
 
} 
 
.face:nth-child(1) { 
 
\t background: rgba(255, 0, 0, 0.2); 
 
} 
 
.face:nth-child(2) { background: rgba(255, 255, 0, 0.2); } 
 
.face:nth-child(3) { background: rgba( 0, 255, 0, 0.2); } 
 
.face:nth-child(4) { background: rgba( 0, 255, 255, 0.2); } 
 
.face:nth-child(5) { background: rgba( 0, 0, 255, 0.2); } 
 
.face:nth-child(6) { background: rgba(255, 0, 255, 0.2); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<body translate="no" onload="init()"> 
 
<div class='cube' id="cubo"> 
 
    <div class='face'><span>1</span></div> 
 
    <div class='face'><span>2</span></div> 
 
    <div class='face'><span>3</span></div> 
 
    <div class='face'><span>4</span></div> 
 
    <div class='face'><span>5</span></div> 
 
    <div class='face'><span>6</span></div> 
 
</div> 
 
</body>

+0

こんにちは!質問のコードを質問エディタツールバーのスニッピット({{}}ボタン)に変換して、質問にコードを埋め込み、実行し、実行可能コードを簡単に回答にコピーすることができます。 – msanford

+1

これは素晴らしいことです!本当にありがとう、私は次回にやるよ! –

答えて

1

私は、これはあなたが望む行動であるべきと考えています。コード内の変数p0の初期化に失敗したため、クリックせずにスクロールしてみるとエラーが発生します。また、onMouseDown関数にp0を設定した後は、マウスが設定したp0値に基づいてスクロールが開始されるように、最初の状態(これはちょうど{x:0, y:0}に設定されています)に戻りません。

詳細については、 $(document).ready()では、p0変数の初期状態をwindow.p0={'x':0,'y':0}に設定しました。さらに、onMouseUp関数のp0変数をリセットして、ドラッグを停止して再びスクロールを開始すると正しく設定されるようにします。

また、私は、キューブフェイスのスタイルの状態の保存を含むようにスクロール機能を変更しました。私は、$(window).scroll関数が適用していたスタイルステートを格納していなかったため、ドラッグが原因で、あなたが気づいていたジャンプを引き起こしていたキューブの最後の状態が本質的に「失われていた」と考えています。この変更は、キューブの面に急速に適用されるスタイルに継続的に追加されるため、オーバーロードが発生する可能性があることに注意してください。

// START OF UNSURE PART 
 

 
$('document').ready(function() { 
 
    var lastScrollTop = 0; 
 
    //Set the initial state of window.p0 so that scrolling works without clicking 
 
    window.p0 = { 
 
    'x': 0, 
 
    'y': 0 
 
    }; 
 
    $(window).scroll(function trucenscroll(event) { 
 
    var st = $(this).scrollTop(); 
 
    var sl = $(this).scrollLeft(); 
 
    if (st > lastScrollTop) { 
 

 
     //Le cube tourne 
 
     var p1, angle, i, tmp; 
 

 
     p1 = { 
 
      'x': sl - p0.x, 
 
      'y': st - p0.y 
 
     }, 
 
     angle = { 
 
      'x': -p1.y * unit, 
 
      'y': p1.x * unit 
 
     }; 
 
     for (i = 0; i < faces.length; i++) { 
 
     tmp = 'rotateX(' + angle.x + 'deg)' + ' rotateY(' + angle.y + 'deg)' + styles[i]; 
 
     faces[i].style.transform = p + tmp; 
 
     faces[i].style['-webkit-transform'] = p + tmp; 
 
     //Save the state of the style of the cube faces. This ensures that if you switch to dragging, then there will be no jumps because all of the transforms will still be correctly applied. 
 
     style = faces[i].style; 
 
     var tmpStyle = style.transform || style['-webkit-transform']; 
 
     styles[i] = tmpStyle.replace('perspective(32em) ', ''); 
 
     } 
 
    } else if (st == lastScrollTop) { 
 
     //do nothing 
 
     //In IE this is an important condition because there seems to be some instances where the last scrollTop is equal to the new one 
 
    } else { 
 
     var p1, angle, i, tmp; 
 
     p1 = { 
 
      'x': sl - p0.x, 
 
      'y': st - p0.y 
 
     }, 
 
     angle = { 
 
      'x': -p1.y * unit, 
 
      'y': p1.x * unit 
 
     }; 
 

 
     for (i = 0; i < faces.length; i++) { 
 
     tmp = 'rotateX(' + angle.x + 'deg)' + ' rotateY(' + angle.y + 'deg)' + styles[i]; 
 
     faces[i].style.transform = p + tmp; 
 
     faces[i].style['-webkit-transform'] = p + tmp; 
 
     //Save the state of the style of the cube faces. This ensures that if you switch to dragging, then there will be no jumps because all of the transforms will still be correctly applied. 
 
     style = faces[i].style; 
 
     var tmpStyle = style.transform || style['-webkit-transform']; 
 
     styles[i] = tmpStyle.replace('perspective(32em) ', ''); 
 
     } 
 
    } 
 
    lastScrollTop = st; 
 
    }); 
 
}); 
 

 

 
// END OF UNSURE PART 
 

 

 

 

 

 

 

 

 
init(); 
 
//=========================================================== 
 
// \t \t \t onMouseMove 
 
//=========================================================== 
 
function onMouseMove(e) { 
 
    var p1, angle, i, tmp; 
 

 
    if (!dragging) return; 
 

 
    p1 = { 
 
     'x': e.clientX - p0.x, 
 
     'y': e.clientY - p0.y 
 
    }, 
 
    angle = { 
 
     'x': -p1.y * unit, 
 
     'y': p1.x * unit 
 
    }; 
 
    for (i = 0; i < faces.length; i++) { 
 
    tmp = 'rotateX(' + angle.x + 'deg)' + ' rotateY(' + angle.y + 'deg)' + styles[i]; 
 
    faces[i].style.transform = p + tmp; 
 
    faces[i].style['-webkit-transform'] = p + tmp; 
 
    } 
 
} 
 
//=========================================================== 
 
// \t \t \t onMouseDown 
 
//=========================================================== 
 
function onMouseDown(e) { 
 
    var element; 
 

 
    onMouseUp(); // disable if dragging 
 

 
    element = e.target; 
 
    //if (! element.classList.contains('face')) return false; 
 

 
    e.preventDefault(); 
 
    window.p0 = { 
 
    'x': e.clientX, 
 
    'y': e.clientY 
 
    }; 
 
    dragging = true; 
 
    return false; 
 
} 
 
//=========================================================== 
 
// \t \t \t onMouseUp 
 
//=========================================================== 
 
function onMouseUp(e) { 
 
    var i, tmp, style; 
 

 
    if (!dragging) return; 
 
    dragging = false; 
 

 
    //Save the state of the style of the cube faces. This ensures that if you switch to dragging, then there will be no jumps because all of the transforms will still be correctly applied. 
 
    for (i = 0; i < faces.length; i++) { 
 
    style = faces[i].style; 
 
    tmp = style.transform || style['-webkit-transform']; 
 
    styles[i] = tmp.replace('perspective(32em) ', ''); 
 
    } 
 
    //Reset the window.p0 variable back for scrolling to work 
 
    window.p0 = { 
 
    'x': 0, 
 
    'y': 0 
 
    }; 
 

 
} 
 
//===================================================================== 
 
// \t \t \t initializeCube 
 
//===================================================================== 
 
function initializeCube() { 
 
    var i, tmp; 
 

 
    for (i = 0; i < faces.length; i++) { 
 
    if (i < 4) tmp = 'rotateY(' + i * 90 + 'deg)'; 
 
    if (i >= 4) tmp = 'rotateX(' + Math.pow(-1, i) * 90 + 'deg)'; 
 
    tmp += ' translateZ(' + side/2 + 'px)'; 
 

 
    faces[i].style.transform = p + tmp; 
 
    faces[i].style['-webkit-transform'] = p + tmp; 
 
    styles.push(tmp); 
 
    } 
 
} 
 
//===================================================================== 
 
// \t \t \t init 
 
//===================================================================== 
 
function init() { 
 
    window.addEventListener('mousedown', onMouseDown, false); 
 
    window.addEventListener('mouseup', onMouseUp, false); 
 
    window.addEventListener('mousemove', onMouseMove, false); 
 

 
    window.faces = document.querySelectorAll('.face'); 
 
    window.styles = new Array(); 
 
    window.style = getComputedStyle(faces[0]); 
 
    window.factor = 3; 
 
    window.side = parseInt(style.width.split('px')[0], 10); 
 
    window.max_amount = factor * side; 
 
    window.unit = 360/max_amount; 
 
    window.dragging = false; 
 
    window.scrolling = false; 
 
    window.p = 'perspective(32em)'; 
 

 
    initializeCube(); 
 
}
body { 
 
    position: relative; 
 
    height: 5000px; 
 
} 
 

 
.cube, 
 
.cube * { 
 
    position: absolute; 
 
    top: 25vh; 
 
    left: 50%; 
 
} 
 

 
.cube { 
 
    user-select: none; 
 
    cursor: move; 
 
} 
 

 
.cube div span { 
 
    position: relative; 
 
    top: 60px; 
 
    left: -5px; 
 
    font-size: 8em; 
 
} 
 

 
.face { 
 
    box-sizing: border-box; 
 
    border: solid 1px; 
 
    margin: -8em; 
 
    width: 16em; 
 
    height: 16em; 
 
    box-shadow: inset 0 0 15px rgba(0, 0, 255, 0.6); 
 
    text-align: center; 
 
    /** backface-visibility: hidden; /**/ 
 
} 
 

 
.face:nth-child(1) { 
 
    background: rgba(255, 0, 0, 0.2); 
 
} 
 

 
.face:nth-child(2) { 
 
    background: rgba(255, 255, 0, 0.2); 
 
} 
 

 
.face:nth-child(3) { 
 
    background: rgba(0, 255, 0, 0.2); 
 
} 
 

 
.face:nth-child(4) { 
 
    background: rgba(0, 255, 255, 0.2); 
 
} 
 

 
.face:nth-child(5) { 
 
    background: rgba(0, 0, 255, 0.2); 
 
} 
 

 
.face:nth-child(6) { 
 
    background: rgba(255, 0, 255, 0.2); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<body translate="no" onload="init()"> 
 
    <div class='cube' id="cubo"> 
 
    <div class='face'><span>1</span></div> 
 
    <div class='face'><span>2</span></div> 
 
    <div class='face'><span>3</span></div> 
 
    <div class='face'><span>4</span></div> 
 
    <div class='face'><span>5</span></div> 
 
    <div class='face'><span>6</span></div> 
 
    </div> 
 
</body>

+0

こんにちは。あなたが少し変更したことを詳しく説明できますか? – msanford

+0

本当にありがとうございました! それはずっと良いですが、スクロールとドラッグを交互に行うと、90%の時間で「ジャンプ」しています。あなたが話しているp0問題を理解しています。時にはうまくいくように思えるので、いつ、どうやって解決しようとしています。 EDIT:ウィンドウがスクロールされてドラッグされているときに、メールが発生するようです。 –

+1

@msanfordこれらの編集があなたの質問に答えるかどうか教えてください。私は変更を加えた特定の場所のコードにコメントを追加しました。 – Joffutt4

関連する問題