admin管理员组文章数量:1291009
So I have a page in HTML
which has a background based on the well-known particle library. I have a button and there is a problem with it: it can be seen but it cannot be clicked. I tried changing the size and the alignment of the background but cannot make it to work... How to make the button clickable?
My code:
HTML:
<div id="particles-js" ><canvas class="particles-js-canvas" width="500" height="1000"></canvas></div>
<button type="button" class="btn btn-info" onclick="alert('hello')">Info</button>
CSS:
body {
background: #212121;
}
.particles-js-canvas{
width: 100%;
height: 100%;
position: fixed;
left: 0px;
right: 0px;
}
And the Javascript
code to initialize the particles:
$(window).on('load', function() {
particlesJS("particles-js", {
"particles": {
"number": {
"value": 400,
"density": {
"enable": true,
"value_area": 400
}
},
"color": {
"value": "#ffffff"
},
"shape": {
"type": "circle",
"stroke": {
"width": 0,
"color": "#000000"
},
"polygon": {
"nb_sides": 4
},
"image": {
"src": ".jpg",
"width": 100,
"height": 100
}
},
"opacity": {
"value": 0.5,
"random": true,
"anim": {
"enable": false,
"speed": 1,
"opacity_min": 0.1,
"sync": false
}
},
"size": {
"value": 2,
"random": true,
"anim": {
"enable": false,
"speed": 40,
"size_min": 0.1,
"sync": false
}
},
"line_linked": {
"enable": false,
"distance": 150,
"color": "#ffffff",
"opacity": 0.4,
"width": 1
},
"move": {
"enable": true,
"speed": 1,
"direction": "bottom",
"random": false,
"straight": true,
"out_mode": "out",
"bounce": false,
"attract": {
"enable": false,
"rotateX": 600,
"rotateY": 1200
}
}
},
"interactivity": {
"detect_on": "canvas",
"events": {
"onhover": {
"enable": false,
"mode": "grab"
},
"onclick": {
"enable": false,
"mode": "repulse"
},
"resize": true
},
"modes": {
"grab": {
"distance": 200,
"line_linked": {
"opacity": 1
}
},
"bubble": {
"distance": 400,
"size": 40,
"duration": 2,
"opacity": 8,
"speed": 3
},
"repulse": {
"distance": 200,
"duration": 0.4
},
"push": {
"particles_nb": 4
},
"remove": {
"particles_nb": 2
}
}
},
"retina_detect": true
});
});
So I have a page in HTML
which has a background based on the well-known particle library. I have a button and there is a problem with it: it can be seen but it cannot be clicked. I tried changing the size and the alignment of the background but cannot make it to work... How to make the button clickable?
My code:
HTML:
<div id="particles-js" ><canvas class="particles-js-canvas" width="500" height="1000"></canvas></div>
<button type="button" class="btn btn-info" onclick="alert('hello')">Info</button>
CSS:
body {
background: #212121;
}
.particles-js-canvas{
width: 100%;
height: 100%;
position: fixed;
left: 0px;
right: 0px;
}
And the Javascript
code to initialize the particles:
$(window).on('load', function() {
particlesJS("particles-js", {
"particles": {
"number": {
"value": 400,
"density": {
"enable": true,
"value_area": 400
}
},
"color": {
"value": "#ffffff"
},
"shape": {
"type": "circle",
"stroke": {
"width": 0,
"color": "#000000"
},
"polygon": {
"nb_sides": 4
},
"image": {
"src": "http://wiki.lexisnexis./academic/images/f/fb/Itunes_podcast_icon_300.jpg",
"width": 100,
"height": 100
}
},
"opacity": {
"value": 0.5,
"random": true,
"anim": {
"enable": false,
"speed": 1,
"opacity_min": 0.1,
"sync": false
}
},
"size": {
"value": 2,
"random": true,
"anim": {
"enable": false,
"speed": 40,
"size_min": 0.1,
"sync": false
}
},
"line_linked": {
"enable": false,
"distance": 150,
"color": "#ffffff",
"opacity": 0.4,
"width": 1
},
"move": {
"enable": true,
"speed": 1,
"direction": "bottom",
"random": false,
"straight": true,
"out_mode": "out",
"bounce": false,
"attract": {
"enable": false,
"rotateX": 600,
"rotateY": 1200
}
}
},
"interactivity": {
"detect_on": "canvas",
"events": {
"onhover": {
"enable": false,
"mode": "grab"
},
"onclick": {
"enable": false,
"mode": "repulse"
},
"resize": true
},
"modes": {
"grab": {
"distance": 200,
"line_linked": {
"opacity": 1
}
},
"bubble": {
"distance": 400,
"size": 40,
"duration": 2,
"opacity": 8,
"speed": 3
},
"repulse": {
"distance": 200,
"duration": 0.4
},
"push": {
"particles_nb": 4
},
"remove": {
"particles_nb": 2
}
}
},
"retina_detect": true
});
});
Share
Improve this question
asked Sep 17, 2018 at 17:32
yanu911yanu911
231 gold badge1 silver badge3 bronze badges
4
- you're missing a ';' when the alert mand ends at alert('hello') – Guy Louzon Commented Sep 17, 2018 at 17:34
- ';' shouldn't be any problem. – samnu pel Commented Sep 17, 2018 at 17:36
- @GuyL tried adding ';' does not work either – yanu911 Commented Sep 17, 2018 at 17:38
- Please, provide a snippet or jsFiddle where we can see and try your code. minimal reproducible example – Calvin Nunes Commented Sep 17, 2018 at 17:39
5 Answers
Reset to default 3Even if you can see your button, that does not mean that the button is in the foreground. Actually, the canvas is hiding your button. You can try adding a z-index to the background:
body {
background: #212121;
}
.particles-js-canvas{
width: 100%;
height: 100%;
position: fixed;
left: 0px;
right: 0px;
z-index: -1;
}
As i understand, this is what you need.
<button type="button" class="btn btn-info" onclick="alert('hello'); setLocation('https://example.');">Info</button>
The problem is that you are using a canvas inside a div tag. This canvas hides access to the button and acts as a layer on top of it.
Add this to the CSS code and it takes care of the rest.
canvas{
z-index:-1;
}
If you are looking to show an alert
add javascript
before alert. Also note inline javascript is not a good practice.Rather you should create a function and avoid using alert as function name since it is window reserved method
<div id="particles-js">
<canvas class="particles-js-canvas" width="100" height="100"></canvas>
</div>
<button type="button" class="btn btn-info" onclick="javascript:alert('hello')">Info</button>
We are using particles.js canvas as an overlay element. Altering the z-index order in this case doesn't help. If the on hover, on click, ie. 'grab, repulse, bubbles' interactivity features of particles.js are not required – It's possible to disable pointer events on the canvas via css, this will allow a user to click through the canvas and interact with underlying elements.
canvas.particles-js-canvas-el {
pointer-events: none;
}
本文标签: javascriptCannot click button in HTMLStack Overflow
版权声明:本文标题:javascript - Cannot click button in HTML? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741517780a2382996.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论