/    Sign up×
Community /Pin to ProfileBookmark

dynamic div height

I found this code online I left the source in it

<html>
<head>
<style>
.staff { margin:10px 0;}
.staff-block{ float: left; width:48%; padding-left: 10px; padding-bottom: 10px;}
.staff-title{ font-family: Verdana, Tahoma, Arial, Serif; background-color: #1162c5; color: white; padding:4px; border: solid 1px #2e3d7a; border-top-left-radius:3px; border-top-right-radius: 6px; font-weight: bold;}
.staff-name { font-family: Myriad Web Pro; font-size: 11pt; line-height:30px; padding: 0 10px;}
.staff-name:hover { background-color: silver !important; cursor: pointer;}
.staff-section { display:inline-block; padding-left: 10px;}
.staff-desc { font-family: Myriad Web Pro; height: 0px; padding: 3px; overflow:hidden; background-color:#def; display: block; border: solid 1px silver;}
.staff-desc p { text-align: justify; margin-top: 5px;}
.staff-desc img { margin: 5px 10px 5px 5px; float:left; height: 185px; }
</style>
</head>
<body>

<!– STOP STAFF SECTION –>

<script src=”http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js”></script>

<script language=”javascript”><!–
//<![CDATA[
$(‘.staff-name’).hover(function() {
$(this).toggleClass(‘hover’);
});
var lastItem;
$(‘.staff-name’).click(function(currentItem) {
var currentItem = $(this);
if ($(this).next().height() == 0) {
$(lastItem).css({‘font-weight’:’normal’});
$(lastItem).next().animate({height: ‘0px’},400,’swing’);
$(this).css({‘font-weight’:’bold’});
[B] $(this).next().animate({height: ‘300px’,opacity: 1},400,’swing’)[/B];
} else {
$(this).css({‘font-weight’:’normal’});
$(this).next().animate({height: ‘0px’,opacity: 1},400,’swing’);
}
lastItem = $(this);
});
//]]>
–></script>

this part set the height $(this).next().animate({height: ‘300px’,opacity: 1},400,’swing’) but instead of the 300 I want the height to be set dynamically
so some div can be less then 300 and some can be more than 300. In other words it will automatically fit the height of the div depending on the amount of text in that div
thanks

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@rootFeb 26.2016 — 
  • 1. JQuery is answered in the JQuery forum, see my signature

  • 2. Wrap your code in forum tags ?


  • as for a dynamic height, that to me sounds more of a CSS issue.
    Copy linkTweet thisAlerts:
    @SempervivumFeb 26.2016 — I recommend posting the URL if your code is online. If not try to create a jsfiddle.

    Does the rest of your code work on click?
    Copy linkTweet thisAlerts:
    @earlcoolsauthorFeb 26.2016 — I recommend posting the URL if your code is online. If not try to create a jsfiddle.

    Does the rest of your code work on click?[/QUOTE]


    [CODE] <html>
    <head>
    <style>
    .staff { margin:10px 0;}
    .staff-block{ float: left; width:48%; padding-left: 10px; padding-bottom: 10px;}
    .staff-title{ font-family: Verdana, Tahoma, Arial, Serif; background-color: #1162c5; color: white; padding:4px; border: solid 1px #2e3d7a; border-top-left-radius:3px; border-top-right-radius: 6px; font-weight: bold;}
    .staff-name { font-family: Myriad Web Pro; font-size: 11pt; line-height:30px; padding: 0 10px;}
    .staff-name:hover { background-color: silver !important; cursor: pointer;}
    .staff-section { display:inline-block; padding-left: 10px;}
    .staff-desc { font-family: Myriad Web Pro; height: 0px; padding: 3px; overflow:hidden; background-color:#def; display: block; border: solid 1px silver;}
    .staff-desc p { text-align: justify; margin-top: 5px;}
    .staff-desc img { margin: 5px 10px 5px 5px; float:left; height: 185px; }
    </style>
    </head>
    <body>



    <!-- STOP STAFF SECTION -->

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    <script language="javascript"><!--
    //<![CDATA[
    $('.staff-name').hover(function() {
    $(this).toggleClass('hover');
    });
    var lastItem;
    $('.staff-name').click(function(currentItem) {
    var currentItem = $(this);
    if ($(this).next().height() == 0) {
    $(lastItem).css({'font-weight':'normal'});
    $(lastItem).next().animate({height: '0px'},400,'swing');
    $(this).css({'font-weight':'bold'});
    $(this).next().animate({height: '300px',opacity: 1},400,'swing');
    } else {
    $(this).css({'font-weight':'normal'});
    $(this).next().animate({height: '0px',opacity: 1},400,'swing');
    }
    lastItem = $(this);
    });
    //]]>
    --></script>

    [/CODE]


    the code works perfectly accept the height to dynamic
    Copy linkTweet thisAlerts:
    @SempervivumFeb 26.2016 — The HTML (content of body) is missing.
    ×

    Success!

    Help @earlcools spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.18,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...