Skip to content

Commit a144c49

Browse files
committed
Merge pull request DefinitelyTyped#1492 from johnnyreilly/master
jQuery: JSDoc'd width / height / innerWidth / innerHeight / coordinates
2 parents f22511d + 925a128 commit a144c49

File tree

2 files changed

+146
-9
lines changed

2 files changed

+146
-9
lines changed

jquery/jquery-tests.ts

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,6 +1642,48 @@ function test_height() {
16421642
});
16431643
}
16441644

1645+
function test_width() {
1646+
// Returns width of browser viewport
1647+
$(window).width();
1648+
1649+
// Returns width of HTML document
1650+
$(document).width();
1651+
1652+
function showWidth(ele, w) {
1653+
$("div").text("The width for the " + ele + " is " + w + "px.");
1654+
}
1655+
$("#getp").click(function () {
1656+
showWidth("paragraph", $("p").width());
1657+
});
1658+
$("#getd").click(function () {
1659+
showWidth("document", $(document).width());
1660+
});
1661+
$("#getw").click(function () {
1662+
showWidth("window", $(window).width());
1663+
});
1664+
1665+
var modWidth = 50;
1666+
$("div").one("click", function () {
1667+
$(this).width(modWidth).addClass("mod");
1668+
modWidth -= 8;
1669+
});
1670+
}
1671+
1672+
function test_coordinates() {
1673+
var p = $("p:last");
1674+
var offset = p.offset();
1675+
p.html("left: " + offset.left + ", top: " + offset.top);
1676+
1677+
$("*", document.body).click(function (event) {
1678+
var offset = $(this).offset();
1679+
event.stopPropagation();
1680+
$("#result").text(this.tagName +
1681+
" coords ( " + offset.left + ", " + offset.top + " )");
1682+
});
1683+
1684+
$("p:last").offset({ top: 10, left: 30 });
1685+
}
1686+
16451687
function test_hide() {
16461688
$('.target').hide();
16471689
$('#clickme').click(function () {
@@ -1751,13 +1793,11 @@ function test_index() {
17511793
function test_innerHeight() {
17521794
var p = $("p:first");
17531795
$("p:last").text("innerHeight:" + p.innerHeight());
1754-
p.innerHeight(p.innerHeight() * 2).innerHeight();
17551796
}
17561797

17571798
function test_innerWidth() {
17581799
var p = $("p:first");
17591800
$("p:last").text("innerWidth:" + p.innerWidth());
1760-
p.innerWidth(p.innerWidth() * 2).innerWidth();
17611801
}
17621802

17631803
function test_outerHeight() {

jquery/jquery.d.ts

Lines changed: 104 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,14 @@ interface JQueryEventConstructor {
348348
new (name: string, eventProperties?: any): JQueryEventObject;
349349
}
350350

351+
/**
352+
* The interface used to specify coordinates.
353+
*/
354+
interface JQueryCoordinates {
355+
left: number;
356+
top: number;
357+
}
358+
351359
/**
352360
* The interface used to specify easing functions.
353361
*/
@@ -972,20 +980,73 @@ interface JQuery {
972980
*/
973981
css(properties: Object): JQuery;
974982

983+
/**
984+
* Get the current computed height for the first element in the set of matched elements.
985+
*/
975986
height(): number;
987+
/**
988+
* Set the CSS height of every matched element.
989+
*
990+
* @param value An integer representing the number of pixels, or an integer with an optional unit of measure appended (as a string).
991+
*/
976992
height(value: number): JQuery;
993+
/**
994+
* Set the CSS height of every matched element.
995+
*
996+
* @param value An integer representing the number of pixels, or an integer with an optional unit of measure appended (as a string).
997+
*/
977998
height(value: string): JQuery;
978-
height(func: (index: any, height: any) => any): JQuery;
999+
/**
1000+
* Set the CSS height of every matched element.
1001+
*
1002+
* @param func A function returning the height to set. Receives the index position of the element in the set and the old height as arguments. Within the function, this refers to the current element in the set.
1003+
*/
1004+
height(func: (index: number, height: number) => number): JQuery;
1005+
/**
1006+
* Set the CSS height of every matched element.
1007+
*
1008+
* @param func A function returning the height to set. Receives the index position of the element in the set and the old height as arguments. Within the function, this refers to the current element in the set.
1009+
*/
1010+
height(func: (index: number, height: string) => string): JQuery;
1011+
/**
1012+
* Set the CSS height of every matched element.
1013+
*
1014+
* @param func A function returning the height to set. Receives the index position of the element in the set and the old height as arguments. Within the function, this refers to the current element in the set.
1015+
*/
1016+
height(func: (index: number, height: string) => number): JQuery;
1017+
/**
1018+
* Set the CSS height of every matched element.
1019+
*
1020+
* @param func A function returning the height to set. Receives the index position of the element in the set and the old height as arguments. Within the function, this refers to the current element in the set.
1021+
*/
1022+
height(func: (index: number, height: number) => string): JQuery;
9791023

1024+
/**
1025+
* Get the current computed height for the first element in the set of matched elements, including padding but not border.
1026+
*/
9801027
innerHeight(): number;
981-
innerHeight(value: number): JQuery;
9821028

1029+
/**
1030+
* Get the current computed width for the first element in the set of matched elements, including padding but not border.
1031+
*/
9831032
innerWidth(): number;
984-
innerWidth(value: number): JQuery;
9851033

986-
offset(): { left: number; top: number; };
987-
offset(coordinates: any): JQuery;
988-
offset(func: (index: any, coords: any) => any): JQuery;
1034+
/**
1035+
* Get the current coordinates of the first element in the set of matched elements, relative to the document.
1036+
*/
1037+
offset(): JQueryCoordinates;
1038+
/**
1039+
* An object containing the properties top and left, which are integers indicating the new top and left coordinates for the elements.
1040+
*
1041+
* @param coordinates An object containing the properties top and left, which are integers indicating the new top and left coordinates for the elements.
1042+
*/
1043+
offset(coordinates: JQueryCoordinates): JQuery;
1044+
/**
1045+
* An object containing the properties top and left, which are integers indicating the new top and left coordinates for the elements.
1046+
*
1047+
* @param func A function to return the coordinates to set. Receives the index of the element in the collection as the first argument and the current coordinates as the second argument. The function should return an object with the new top and left properties.
1048+
*/
1049+
offset(func: (index: number, coords: JQueryCoordinates) => JQueryCoordinates): JQuery;
9891050

9901051
outerHeight(includeMargin?: boolean): number;
9911052
outerHeight(value: number, includeMargin?: boolean): JQuery;
@@ -1001,10 +1062,46 @@ interface JQuery {
10011062
scrollTop(): number;
10021063
scrollTop(value: number): JQuery;
10031064

1065+
/**
1066+
* Get the current computed width for the first element in the set of matched elements.
1067+
*/
10041068
width(): number;
1069+
/**
1070+
* Set the CSS width of each element in the set of matched elements.
1071+
*
1072+
* @param value An integer representing the number of pixels, or an integer along with an optional unit of measure appended (as a string).
1073+
*/
10051074
width(value: number): JQuery;
1075+
/**
1076+
* Set the CSS width of each element in the set of matched elements.
1077+
*
1078+
* @param value An integer representing the number of pixels, or an integer along with an optional unit of measure appended (as a string).
1079+
*/
10061080
width(value: string): JQuery;
1007-
width(func: (index: any, height: any) => any): JQuery;
1081+
/**
1082+
* Set the CSS width of each element in the set of matched elements.
1083+
*
1084+
* @param func A function returning the width to set. Receives the index position of the element in the set and the old width as arguments. Within the function, this refers to the current element in the set.
1085+
*/
1086+
width(func: (index: number, width: number) => number): JQuery;
1087+
/**
1088+
* Set the CSS width of each element in the set of matched elements.
1089+
*
1090+
* @param func A function returning the width to set. Receives the index position of the element in the set and the old width as arguments. Within the function, this refers to the current element in the set.
1091+
*/
1092+
width(func: (index: number, width: string) => string): JQuery;
1093+
/**
1094+
* Set the CSS width of each element in the set of matched elements.
1095+
*
1096+
* @param func A function returning the width to set. Receives the index position of the element in the set and the old width as arguments. Within the function, this refers to the current element in the set.
1097+
*/
1098+
width(func: (index: number, width: string) => number): JQuery;
1099+
/**
1100+
* Set the CSS width of each element in the set of matched elements.
1101+
*
1102+
* @param func A function returning the width to set. Receives the index position of the element in the set and the old width as arguments. Within the function, this refers to the current element in the set.
1103+
*/
1104+
width(func: (index: number, width: number) => string): JQuery;
10081105

10091106
// Data
10101107
clearQueue(queueName?: string): JQuery;

0 commit comments

Comments
 (0)