简体   繁体   English

我用这个jQuery代码做错了什么?

[英]What am I doing wrong with this jQuery code?

I currently have some jQuery/JavaScript code that detects the device orientation and modifies the CSS for the navbar accordingly to the device orientation. 我目前有一些jQuery / JavaScript代码,用于检测设备方向,并根据设备方向修改导航栏的CSS。 It works perfectly when you open the navbar in the device orientation but when the navbar is already open and you change the device orientation it breaks and stays the same as the previous orientation. 当您以设备方向打开导航栏时,它非常理想,但是当导航栏已经打开并且您更改设备方向时,它会中断并保持与以前的方向相同。

I've already tried a few if/else statements and a few do/while statements but could not get them how I would like it. 我已经尝试了一些if / else语句和一些do / while语句,但是无法让它们得到我想要的样子。

My jQuery/JavaScript 我的jQuery / JavaScript

 $(document).ready(function() { $(".closebtn, .navbar-fullscreen-background").on('touchstart', function() { $('.navbar-background').css({ 'width': 90 + 'px', 'height': 90 + 'px', 'left': 2 + 'vw', 'top': 30 + 'px', }); $('.fadein-test, .fadein-test2, .fadein-test3, .fadein-test4').hide(); setTimeout(function() { $('.mobile-navbar-button, .bar-1, .bar-2').fadeIn(200); }, 300); $('.navbar-fullscreen-background').fadeOut(300); $('.closebtn').css('display', 'none'); }); $('.fadein-test, .closebtn').hide(); function doOnOrientationChange() { switch (window.orientation) { case 90: $(".mobile-navbar-button").on('touchstart', function() { $('.navbar-background').css({ 'width': 1450 + 'px', 'height': 1200 + 'px', 'left': -400 + 'px', 'top': -750 + 'px', }); $('.navbar-fullscreen-background').fadeIn(300).css('display', 'block'); $('.mobile-navbar-button, .bar-1, .bar-2').css('display', 'none'); $('.fadein-test').show().addClass('fadein-animationtest'); $('.fadein-test2').show().addClass('fadein-animationtest2'); $('.fadein-test3').show().addClass('fadein-animationtest3'); $('.fadein-test4').show().addClass('fadein-animationtest4'); setTimeout(function() { $('.closebtn').fadeIn(200); }, 300); }); break; case -90: $(".mobile-navbar-button").on('touchstart', function() { $('.navbar-background').css({ 'width': 1450 + 'px', 'height': 1200 + 'px', 'left': -400 + 'px', 'top': -750 + 'px', }); $('.navbar-fullscreen-background').fadeIn(300).css('display', 'block'); $('.mobile-navbar-button, .bar-1, .bar-2').css('display', 'none'); $('.fadein-test').show().addClass('fadein-animationtest'); $('.fadein-test2').show().addClass('fadein-animationtest2'); $('.fadein-test3').show().addClass('fadein-animationtest3'); $('.fadein-test4').show().addClass('fadein-animationtest4'); setTimeout(function() { $('.closebtn').fadeIn(200); }, 300); }); break; case 0: $(".mobile-navbar-button").on('touchstart', function() { $('.navbar-background').css({ 'width': 1450 + 'px', 'height': 1200 + 'px', 'left': -400 + 'px', 'top': -400 + 'px', }); $('.navbar-fullscreen-background').fadeIn(300).css('display', 'block'); $('.mobile-navbar-button, .bar-1, .bar-2').css('display', 'none'); $('.fadein-test').show().addClass('fadein-animationtest'); $('.fadein-test2').show().addClass('fadein-animationtest2'); $('.fadein-test3').show().addClass('fadein-animationtest3'); $('.fadein-test4').show().addClass('fadein-animationtest4'); setTimeout(function() { $('.closebtn').fadeIn(200); }, 300); }); break; case 180: $(".mobile-navbar-button").on('touchstart', function() { $('.navbar-background').css({ 'width': 1450 + 'px', 'height': 1200 + 'px', 'left': -400 + 'px', 'top': -400 + 'px', }); $('.navbar-fullscreen-background').fadeIn(300).css('display', 'block'); $('.mobile-navbar-button, .bar-1, .bar-2').css('display', 'none'); $('.fadein-test').show().addClass('fadein-animationtest'); $('.fadein-test2').show().addClass('fadein-animationtest2'); $('.fadein-test3').show().addClass('fadein-animationtest3'); $('.fadein-test4').show().addClass('fadein-animationtest4'); setTimeout(function() { $('.closebtn').fadeIn(200); }, 300); }); break; default: break; } } window.addEventListener('orientationchange', doOnOrientationChange); // Initial execution if needed doOnOrientationChange(); }); 

I would like it to be able to detect the device orientation change and change the CSS accordingly while the navbar is open as well and not change the CSS to just that CSS for that orientation before it is opened. 我希望它能够检测到设备方向的变化并在导航栏也打开时相应地更改CSS,并且在打开之前不将CSS更改为该方向的CSS。 Thank you. 谢谢。

As I read through your code, I think most of it could be done with CSS instead of JS , using the @media (orientation: landscape) {} /* or portrait */ : https://developer.mozilla.org/en-US/docs/Web/CSS/@media/orientation 当我阅读您的代码时,我认为大部分可以使用CSS而不是JS来完成,可以使用@media (orientation: landscape) {} /* or portrait */ /:https://developer.mozilla.org/en -US /文档/网络/ CSS / @媒体/方向

EDIT 编辑

Anyways, I think restructuring the code may make it a bit easier to understand where it breaks: 无论如何,我认为重组代码可能使它更容易理解中断之处:

 $(document).ready(function() { // starting background var bgNavInitObj = { 'width': 90 + 'px', 'height': 90 + 'px', 'left': 2 + 'vw', 'top': 30 + 'px', }; // 90 and -90 positions var bgNav90 = { 'width': 1450 + 'px', 'height': 1200 + 'px', 'left': -400 + 'px', 'top': -750 + 'px', }; // 0 and 180 positions var bgNav0 = { 'width': 1450 + 'px', 'height': 1200 + 'px', 'left': -400 + 'px', 'top': -400 + 'px', }; // setting transitions function setTransitions() { $('.navbar-fullscreen-background').fadeIn(300).css('display', 'block'); $('.mobile-navbar-button, .bar-1, .bar-2').css('display', 'none'); $('.fadein-test').show().addClass('fadein-animationtest'); $('.fadein-test2').show().addClass('fadein-animationtest2'); $('.fadein-test3').show().addClass('fadein-animationtest3'); $('.fadein-test4').show().addClass('fadein-animationtest4'); setTimeout(function() { $('.closebtn').fadeIn(200); }, 300); } function changeBg() { switch (window.orientation) { case 90: $(".mobile-navbar-button").on('touchstart', function() { $('.navbar-background').css(bgNav90); }); break; case -90: $(".mobile-navbar-button").on('touchstart', function() { $('.navbar-background').css(bgNav90); }); break; case 0: $(".mobile-navbar-button").on('touchstart', function() { $('.navbar-background').css(bgNav0); }); break; case 180: $(".mobile-navbar-button").on('touchstart', function() { $('.navbar-background').css(bgNav0); }); break; default: break; } // this function is always executed setTransitions(); } $(".closebtn, .navbar-fullscreen-background").on('touchstart', function() { $('.navbar-background').css(bgNavInitObj); $('.fadein-test, .fadein-test2, .fadein-test3, .fadein-test4').hide(); setTimeout(function() { $('.mobile-navbar-button, .bar-1, .bar-2').fadeIn(200); }, 300); $('.navbar-fullscreen-background').fadeOut(300); $('.closebtn').css('display', 'none'); }); $('.fadein-test, .closebtn').hide(); // this is your touchstart function // with the changeBg() it uses the needed functions depending on // window.orientation $(".mobile-navbar-button").on('touchstart', function() { changeBg(); }); // this may be a code smell, but I supposed that you may want to // do other things on orientation change function doOnOrientationChange() { changeBg(); } window.addEventListener('orientationchange', doOnOrientationChange); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 

Since you don't do anything in doOrientationChange() other than add new event listeners I think you should get rid of it completely. 由于除了添加新的事件侦听器之外,您在doOrientationChange()除了执行其他任何操作外,我认为您应该完全摆脱它。

Instead use one listener and each time the touchstart occurs store the current orientation. 取而代之的是使用一个侦听器,并且每次触摸开始时都会存储当前方向。 Then when it closes check if orientation is the same. 然后在关闭时检查方向是否相同。 I used jQuery data() to store and get the previous state 我使用jQuery data()来存储并获取先前的状态

It's hard to help rewrite this without seeing the markup so here's a basic high level conceptual outline using some pseudo code. 很难在没有看到标记的情况下重写它,因此这是使用一些伪代码的基本高级概念大纲。 Hopefully this will give you some ideas to work with that are far easier to debug as well as to DRY out the code 希望这会给您一些想法,使您可以更轻松地调试和干燥代码

// cache references to elements in variables for code simplification 
// and not needing to search dom each time and much easier to write 
// variables than long selectors repeatedly

var $nav_parent = $(selector??)
  .data('navState', { open: false, orient: window.orientation}),
  // store state on parent or button?
  $nav_bg = $('.navbar-background'),
  $nav_btn = $(".mobile-navbar-button");

$nav_bg.on('touchstart', function(evt) {
  var prevState = getNavState(),
    prevOrient = prevState.orient,
    currOrient = window.orientation;

  setNavState(currOrient, false)
  $nav_bg.css(getNavBgCss(currOrient, prevOrient, false));

});

$nav_btn.on('touchstart', function(evt) {
  var prevState = getNavState(),
    prevOrient = prevState.orient,
    currOrient = window.orientation;    

  // store state when it opens
  setNavState(currOrient, true);

  $nav_bg.css(getNavBgCss(currOrient, prevOrient, true));

});

// helper functions
function getNavState() {
  // want to store somehwere else? easier to change in one helper function than numerous places
  return $nav_parent.data('navState')
}

function setNavState(orient, isOpen) {
  var currState = {
    orient: orient,
    open: isOpen
  };
  $nav_parent.data('navState', currState)

}
// this ones a bit ugly...but gives ideas on how to manage various conditions
function getNavBgCss(orient, prevOrient, isOpen) {
  // now have access to all possible conditions
  var isSame = orient === prevOrient,
    is90 = Math.abs(orient) === 90;

  // adjust based on orientation differences , not real logic, only conceptual

  return {
    'width': 1450, // no need for 'px' prefix
    'height': 1200,
    'left': is90 ? isSame ? -700 : -200 : isSame ? -200 : 700, // 
    'top': -750,
  }

}

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

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