首页 > 解决方案 > Jquery function with two events using &&

问题描述

$(document).ready(function () {
  var scroll_start = 0;
  var startchange = $('#heroSection');
  var offset = startchange.offset();
  $(document).scroll(function () {
    scroll_start = $(this).scrollTop();
    if (scroll_start > offset.top) && ($!(window).width()<760){
      $('#myTopnav').css('background-color', 'rgba(0, 0, 0, 0.9)');
    } else {
      $('#myTopnav').css('background-color', 'transparent');
    }

  });
}); 

I'm trying to get this function to work only when there is a scroll event and the window is larger than 760px wide but can't seem to crack it.

标签: javascriptjqueryevents

解决方案


Try it like this

$(document).ready(function () {
    var scroll_start = 0;
    var startchange = $('#heroSection');
    var offset = startchange.offset();
    $(document).scroll(function () {
        scroll_start = $(this).scrollTop();
        if ((scroll_start > offset.top) && window.width > 760) {
            $('#myTopnav').css('background-color', 'rgba(0, 0, 0, 0.9)');
        } else {
            $('#myTopnav').css('background-color', 'transparent');
        }

    });
});

You missed a "(" here

if **(**(scroll_start > offset.top) && window.width > 760){

推荐阅读