简体   繁体   English

jQuery .css底部属性未将CSS应用于元素

[英]JQuery .css bottom property not applying css to element

   var startingWindowSize = $(window).width();
   var startPercent = 70;
    if( startingWindowSize >= 1920 )
    {
        startPercent = 70; 
        $('.rsImg').css({bottom: startPercent+"%"});    
    }
    else if( startingWindowSize >= 1440 )
    {
        startPercent = 40;  
        $('.rsImg').css({bottom: startPercent+"%"});   
    }
    else if( startingWindowSize >= 1280 )
    {
        startPercent = 18; 
        $('.rsImg').css({bottom: startPercent+"%"});        
    }
    else
    {
        startPercent = 10; 
        $('.rsImg').css({bottom: startPercent+"%"});        
    }

Why would this code not be working? 为什么此代码不起作用? It refuses to add the style to the tag. 它拒绝将样式添加到标签中。 There is already some css being added before this code before this snippet by a javascript slider plugin. javascript滑块插件在此代码段之前的此代码之前已经添加了一些CSS。 Could this be the cause? 这可能是原因吗? The slider can be found here . 滑块可在此处找到。

Are you sure $('.rsImg') actually refers to the correct item? 您确定$('.rsImg')实际上是指正确的项目吗?
Try alert($('.rsImg').size()) to double-check that it actually found what you think it did. 尝试alert($('.rsImg').size())仔细检查它是否确实找到了您认为的功能。

Also, are you sure this code is running after the page has finished loading? 另外,您确定页面加载完成后此代码正在运行吗? Is it in an onready callback function? 它是在onready回调函数中吗?

This isn't entirely related, but your code can be simplified by only doing the .css() call once: 这并不完全相关,但是只需执行一次.css()调用即可简化代码:

var startingWindowSize = $(window).width();
var startPercent = 10;

if (startingWindowSize >= 1920) startPercent = 70; 
else if (startingWindowSize >= 1440) startPercent = 40;  
else if (startingWindowSize >= 1280) startPercent = 18; 

$('.rsImg').css({bottom: startPercent+"%"});        

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

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