getting screen width into javascript variable and sending it through ajax to a php page to avoid page load

后端 未结 3 464
我在风中等你
我在风中等你 2021-01-16 16:39

This is the JS to detect the screen resolution on my page naming index.html and sending it to php, so that the values can be retrived using $

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-16 17:03

    I'm not sure if I understood your problem but this is what I've got: if you only intend to get the screen width and then set the class I disencourage you to do it that way. It's not nice and in the future if you need to change your page layout you'll be doomed by having all those echo in php and you'll avoid a lot of redirects (index.html -> process.php -> index.php).

    By your example you can do it all in Javascript.

    This is example is with JQuery

    var width = $(window).width(); //get the width of the screen
    $('#Wrapper').css("width",width); //set the width to the class
    

    If the idea is for very different screen resolutions (from PCs to SmartPhones), you should consider changing your approach.

    Good luck :)


    TRY THIS: (I believe is what you want)

    You can do this with just JS and CSS. Here is an example:

    set the style for your body margin and your test div:

    
    

    then add this script:

    
    

    and test it with this html:

    
    
    test
    test
    test
    test
    test
    test
    test
    test
    test

    It works in different resolutions.

    Give it a try :)

提交回复
热议问题