5
In a project that aims to work only on desktop devices (desktop PC or notebook) it is mandatory to use a mouse (or pointing device, which can be a touchpad).
If the user accesses from a mobile device (touch), I redirect to a page stating that the page is only functional on a desktop device. That’s not the problem.
The goal is that if the user accesses from a desktop without a pointing device, all page elements are removed and displayed a warning box as below:
$(function(){
// if(aqui verifico se existe um mouse!){
// se não existe, removo os elementos da página
// e aplico as propriedades abaixo:
$("#bg").css("filter", "blur(5px)");
$("#alerta").show();
// }
});
body{
margin: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
#bg{
position: fixed;
width: 100vw;
height: 100vh;
background-image: url(https://images.unsplash.com/photo-1493246507139-91e8fad9978e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80);
background-size: cover;
}
#alerta{
display: none;
position: fixed;
width: 300px;
padding: 20px;
background-color: #FFFEC5;
border: 1px solid #000;
text-align: center;
}
#alerta h2{
margin-top: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="bg"></div>
<div id="alerta">
<h2>Não foi detectado um mouse!</h2>
É necessário um dispositivo apontador pra usar este site. Conecte-o à uma porta USB e tecle F5.
</div>
What I need is to check on this if
below the absence of the pointing device:
if(mouse não existe){
// código pra remover todos os elementos
// e aplicar as propriedades abaixo:
$("#bg").css("filter", "blur(5px)");
$("#alerta").show();
}
It is possible to check - and how, in Javascript, the absence of the mouse or mouse icon on the screen to then run the code inside the if
quoted?
It would not be easier to try to detect the first interaction and check if it is a touch mouse?
– gato
In case it is touch, I considered it as a mobile device and do a redirect. This is because it can have touch screen desktops. What you really need is to be desktop and have a mouse on the screen.
– Sam
To know if you have a mouse just listen to the event
onmousemove
because the mouse you have the movements, different from the touch.– gato
But can’t take the width of the device? If width is such do it otherwise do it.
– LeAndrade
@Leandrade I believe that the width does not determine the type of device. But the type of device is no problem, this I have done and it works smoothly.
– Sam
@cat But the
mousemove
not only is fired when moving the pointer? How would I detect this in page loading if there is no mouse?– Sam
@Sam if there is a mouse you know the user is to use only keyboard or touch.
– gato
@Sam take a look at this reply.
– gato
@Obg cat! I managed to find the solution and posted as an answer. Obg for the help!
– Sam
I’ll give you an answer with just css, it’s time to eat two slices of pizza ;D
– hugocsl
I did some tests, I could only make the desktop recognize that it is without the mouse, but make the Smartphone recognize that is "without the finger" that is the problem... So if the guy is on the desktop without the mouse I can show or hide a div, but on the smartphone I couldn’t, it always detects an input device. If you want to test there is the link of what I used. Tip, to test after taking the mouse out of destop you have to open in another window, only F5 does not help https://developer.mozilla.org/en-US/docs/Web/CSS/@media/any-Pointer
– hugocsl
@hugocsl It is pq vc did not understand the question rs. : D Forgets smartphone, the problem is desktop exclusive.
– Sam
Oops, so I’m back to the RSS game, soon put an answer, the test is simple but turned out pretty cool eh a technique very unusual but sure and eh pratico
– hugocsl