J'ai une fonction qui détermine la largeur de ma timeline. Je veux définir la largeur pour les ordinateurs de bureau et les appareils mobiles séparément. Comment puis-je les écrire que si le bureau utilise donc ce code si mobile, utilisez un autre code?JS détecter si mobile et définir la condition
If(mobile) { use js function for mobile } else { use js function for desktop }
Voici js fucntion pour le bureau
\t function setTimelineWidth(timelineComponents, width) {
//Line 3 is set width 100%
\t \t \t totalWidth = $(window).width((timelineComponents['timelineDates'].length * 100));
\t \t \t //}
\t \t timelineComponents['eventsWrapper'].css('width', totalWidth+'px');
\t \t updateFilling(timelineComponents['eventsWrapper'].find('a.selected'), timelineComponents['fillingLine'], totalWidth);
\t \t updateTimelinePosition('next', timelineComponents['eventsWrapper'].find('a.selected'), timelineComponents);
\t
\t \t return totalWidth;
\t }
Et voici fonction js pour la largeur mobile
\t function setTimelineWidth(timelineComponents, width) {
\t \t var timeSpan = daydiff(timelineComponents['timelineDates'][0], timelineComponents['timelineDates'][timelineComponents['timelineDates'].length-1]),
\t \t \t timeSpanNorm = timeSpan/timelineComponents['eventsMinLapse'],
\t \t \t timeSpanNorm = Math.round(timeSpanNorm) + 4,
\t \t \t totalWidth = timeSpanNorm*width;
\t \t timelineComponents['eventsWrapper'].css('width', totalWidth+'px');
\t \t updateFilling(timelineComponents['eventsWrapper'].find('a.selected'), timelineComponents['fillingLine'], totalWidth);
\t \t updateTimelinePosition('next', timelineComponents['eventsWrapper'].find('a.selected'), timelineComponents);
\t
\t \t return totalWidth;
\t }
Voici une grande réponse pour [comment détecter un appareil mobile] (https://stackoverflow.com/questions/3514784/what-is-the-best-way-to-detect-a-mobile-device-in-jquery) – Zevgon
@Zevgon oui je l'ai vu. mais si je l'ajoute, ça ne marche pas. Si j'écris 'if (/ Android | webOS | iPhone | iPad | iPod | BlackBerry | IEMobile | Opera Mini/i.test (navigator.userAgent)) { MON FONCTION JS POUR MOBILE }' cela ne fonctionne pas – Namik