Script ne fonctionne pas sur mobile
Bonjour,
J'essaie de mettre en place une "fenêtre" avec différentes div qui contiendront des pages.
deux flèches dirigent le mouvement des div mais aucune fonction de la classe ne répondent en mode mobile
Merci de me d'aiguiller sur une solution
voici mon script de test
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
| <!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title></title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="robots" content="noindex, nofollow">
<meta name="googlebot" content="noindex, nofollow">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<style>
div.center {
width: 200px; padding:2px;
height: 210px;
border: 0px solid #000;
background:#000000;
margin: auto;
overflow-x: hidden;
overflow-y: hidden;
white-space: nowrap;
}
div.internal {
display: inline-block;
vertical-align: middle;
width: 200px;
text-align: center;
color:#ffffff;
}
</style>
</head>
<body>
<div class="horizon horizon-prev"> <img src="../images/left-arrow.png" /></div>
<div class="horizon horizon-next"><img src="../images/right-arrow.png" /></div>
<div class="center" id="content">
<div class=internal>
<div style="margin:0 auto; border:1px solid #ffffff;">div 1</div>
</div>
<div class=internal>
<div style="margin:0 auto; border:1px solid #ff0000;">div 2</div>
</div>
<div class=internal>
<div style="margin:0 auto; border:1px solid #ffffff;">div 3</div>
</div>
<div class=internal>
div 4
</div>
<div class=internal>
div 5
</div>
<div class=internal>
div 6
</div>
<div class=internal>
div 7
</div>
<div class=internal>
div 8
</div>
</div>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
class HorizontalScrollDragger {
constructor(element) {
this.slider = typeof element === 'string' ? document.querySelector(element) : element instanceof Element ? element : null;
if (this.slider === null) return console.error('HorizontalScrollDragger: Element not found');
this.isDown = false;
this.preventClick = false;
this.startX;
this.scrollLeft;
this.init();
}
init() {
this.slider.addEventListener('mousedown', (e) => this.handleMouseDown(e));
this.slider.addEventListener('mouseleave', () => this.handleMouseLeave());
this.slider.addEventListener('mouseup', () => this.handleMouseUp());
this.slider.addEventListener('mousemove', (e) => this.handleMouseMove(e));
this.slider.addEventListener('click', (e) => this.handleClick(e));
}
handleMouseDown(e) {
console.log('down');
this.isDown = true;
// this.slider.classList.add('active');
this.startX = e.pageX - this.slider.offsetLeft;
this.scrollLeft = this.slider.scrollLeft;
this.preventClick = false;
}
handleMouseLeave() {
this.isDown = false;
// this.slider.classList.remove('active');
}
handleMouseUp() {
this.isDown = false;
// this.slider.classList.remove('active');
alert("startX : "+this.startX+" / "+this.slider.offsetLeft);
}
handleMouseMove(e) {
console.log('Move');
if (!this.isDown) return;
e.preventDefault();
const x = e.pageX - this.slider.offsetLeft;
const walk = (x - this.startX) * 3; //scroll-fast
this.slider.scrollLeft = this.scrollLeft - walk;
this.preventClick = true;
}
handleClick(e) {
console.log('Click');
if (this.preventClick) {
e.preventDefault();
}
}
}
new HorizontalScrollDragger('.center');
$('.horizon-prev').click(function() {
event.preventDefault();
$('#content').animate({
scrollLeft: "-=204px"
}, "slow");
});
$('.horizon-next').click(function() {
event.preventDefault();
$('#content').animate({
scrollLeft: "+=204px"
}, "slow");
});
</script>
</body>
</html> |