简体   繁体   English

移动和台式机的不同事件

[英]Different Events for mobile and desktop

I want to have when a user clicks a login icon on desktop it would have a modal dialog show up. 我希望当用户单击桌面上的登录图标时,它会显示一个模式对话框。 However on smaller devices like mobile and tablets, I want a reactjs route to kick in and show in my "main" content area and not use a modal dialog. 但是,在较小的设备(例如移动设备和平板电脑)上,我希望有Reactjs路由加入并显示在“主要”内容区域中,而不要使用模式对话框。

I am not sure how to do this cleanly, first I was thinking maybe there are 2 clicks, one for touch and one for click but what happens on ipad where it might be big enough to show a dialog and should go that way. 我不确定如何干净地执行此操作,首先我在想也许有2次点击,一次是触摸,一次是点击,但是在ipad上发生了什么,它可能足够大以显示对话框,应该这样。

Next I was thinking of just having 2 separate icons and depending on my media query it shows the right one, though I just don't like the duplicate code. 接下来,我考虑仅使用2个单独的图标,并根据媒体查询显示正确的图标,尽管我只是不喜欢重复的代码。

Any other ways? 还有其他方法吗? I don't want to start writing size logic in my java script code as everything else is being handled in media queries right now. 我不想开始在Java脚本代码中编写大小逻辑,因为其他所有内容现在都在媒体查询中处理。

You can use a single icon. 您可以使用一个图标。

You can use matchMedia - the javascript equivalent of CSS @media queries - to determine whether the browser viewport is larger or smaller and then add a class to the icon to record this. 您可以使用matchMedia (相当于CSS @media查询的javascript)确定浏览器视口是大还是小,然后将一个类添加到图标中进行记录。

When the icon is clicked: 单击图标时:

  • if it contains the .larger-viewport class, it will bring up the modal dialog 如果包含.larger-viewport类,则将弹出模式对话框
  • if it contains the .smaller-viewport class, it will fire the reactjs. 如果包含.smaller-viewport类,则将触发reactjs。

 // Initialise Variables var iconForModal; var iconForReact; var loginIcon = document.getElementsByClassName('login-icon')[0]; // Check and record viewport size function checkViewportSize() { if (window.matchMedia("(max-width: 800px)").matches) { loginIcon.className = 'login-icon larger-viewport'; } else { loginIcon.className = 'login-icon smaller-viewport'; } } window.addEventListener('resize', checkViewportSize, false); window.addEventListener('load', checkViewportSize, false); // Activate Login function activateLogin() { if (loginIcon.classList.contains('larger-viewport')) { console.log('Login Modal Activated'); } if (loginIcon.classList.contains('smaller-viewport')) { console.log('Login React Activated') } } loginIcon.addEventListener('click', activateLogin, false); 
 <button type="button" class="login-icon">Login</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM