如何将额外的if语句添加到现有的javascript函数中以获得屏幕宽度?

时间:2017-01-26 21:48:27

标签: javascript html css if-statement

我有一个Javascript代码,我想为if语句添加一个额外的规则 - 如果屏幕宽度低于767px,那么保持CSS类属性'av_header_transparency'以下是有问题的代码:

public class MainActivity extends AppCompatActivity
{

    private TextView mInfo;
    private Logger mLogger;
    private HashMap<UsbDevice, UsbDataBinder> mHashMap = new HashMap<UsbDevice, UsbDataBinder>();
    private UsbManager mUsbManager;
    private PendingIntent mPermissionIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mInfo = (TextView)findViewById(R.id.log);

        mLogger = new Logger(this);
        mLogger.setMode(Logger.MODE_TOAST);

        mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);

        usbConnection();
    }

    private void usbConnection() {
        IntentFilter filter = new IntentFilter(UsbManager.ACTION_USB_DEVICE_ATTACHED);
        registerReceiver(mUsbAttachReceiver , filter);
        filter = new IntentFilter(UsbManager.ACTION_USB_DEVICE_DETACHED);
        registerReceiver(mUsbDetachReceiver , filter);

        mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
        filter = new IntentFilter(ACTION_USB_PERMISSION);
        registerReceiver(mUsbReceiver, filter);

        showDevices();
    }

    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mUsbDetachReceiver);
        unregisterReceiver(mUsbAttachReceiver);
        unregisterReceiver(mUsbReceiver);
    };

    BroadcastReceiver mUsbDetachReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
                UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                if (device != null) {
                    // call your method that cleans up and closes communication with the device
                    UsbDataBinder binder = mHashMap.get(device);
                    if (binder != null) {
                        binder.onDestroy();
                        mHashMap.remove(device);
                        Toast.makeText(MainActivity.this, "Attached!", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        }
    };

    BroadcastReceiver mUsbAttachReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
                showDevices();
                Toast.makeText(MainActivity.this, "Detached!", Toast.LENGTH_SHORT).show();
            }
        }
    };

    private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
    private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {

        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (ACTION_USB_PERMISSION.equals(action)) {
                synchronized (this) {
                    UsbDevice device = (UsbDevice) intent
                            .getParcelableExtra(UsbManager.EXTRA_DEVICE);

                    if (intent.getBooleanExtra(
                            UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                        if (device != null) {
                            // call method to set up device communication
                            UsbDataBinder binder = new UsbDataBinder(mUsbManager, device);
                            mHashMap.put(device, binder);
                            Toast.makeText(MainActivity.this, "Permission Granted!", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        // Log.d(TAG, "permission denied for device " + device);
                    }
                }
            }
        }
    };

    private void showDevices() {
        HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
        Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
        while(deviceIterator.hasNext()){
            UsbDevice device = deviceIterator.next();
            mUsbManager.requestPermission(device, mPermissionIntent);
            //your code
            mLogger.log("usb", "name: " + device.getDeviceName() + ", " +
                    "ID: " + device.getDeviceId());
            mInfo.append(device.getDeviceName() + "\n");
            mInfo.append(device.getDeviceId() + "\n");
            mInfo.append(device.getDeviceProtocol() + "\n");
            mInfo.append(device.getProductId() + "\n");
            mInfo.append(device.getVendorId() + "\n");
        }
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

    }
}

目前此代码正在删除CSS类'av_header_transparency',如果向下滚动50px,但我想将其保留在移动屏幕上。这可能吗?

此致

2 个答案:

答案 0 :(得分:1)

CSS媒体查询非常适合:

&#13;
&#13;
@media screen and (max-width:767px) {

  header.someClass {
    /* styles for screeens up to 767px */
  }

}

@media screen and (min-width:768px) {

  header.someClass {
    /* styles for screeens larger than 768px */
  }

}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

不确定它是否正确解释,但你可以在删除它之前检查屏幕宽度,所以像

if(transparent)
{   
    if(st > 50 && window.innerWidth >767)
    {   
                //header.removeClass('av_header_transparency');
        av_change_class(header, 'remove', 'av_header_transparency');
    }
    else
    {
        //header.addClass('av_header_transparency');
        av_change_class(header, 'add', 'av_header_transparency');
    }
}