Skip to content

Commit 1639232

Browse files
committed
�[200~ Window sizes and scrolling
1 parent 8957ff7 commit 1639232

1 file changed

Lines changed: 69 additions & 70 deletions

File tree

  • 2-ui/1-document/10-size-and-scroll-window
Lines changed: 69 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,50 @@
1-
# Window sizes and scrolling
1+
# أحجام النوافذ والتمرير
22

3-
How do we find the width and height of the browser window? How do we get the full width and height of the document, including the scrolled out part? How do we scroll the page using JavaScript?
3+
كيف نجد عرض وارتفاع نافذة المتصفح؟ كيف نحصل على العرض والارتفاع الكاملين للوثيقة ، بما في ذلك الجزء المسحوب؟ كيف ننتقل الصفحة باستخدام جافا سكريبت؟
44

5-
For most such requests, we can use the root document element `document.documentElement`, that corresponds to the `<html>` tag. But there are additional methods and peculiarities important enough to consider.
5+
بالنسبة لمعظم هذه الطلبات ، يمكننا استخدام عنصر المستند الجذر `document.documentElement` ، الذي يتوافق مع العلامة` <html> `. ولكن هناك طرق وخصائص إضافية مهمة بما يكفي للنظر فيها.
66

7-
## Width/height of the window
7+
## عرض / ارتفاع النافذة
88

9-
To get window width and height we can use `clientWidth/clientHeight` of `document.documentElement`:
9+
للحصول على عرض النافذة وارتفاعها ، يمكننا استخدام `clientWidth / clientHeight` من` document.documentElement`:
1010

1111
![](document-client-width-height.svg)
1212

1313
```online
14-
For instance, this button shows the height of your window:
14+
على سبيل المثال ، يعرض هذا الزر ارتفاع النافذة:
1515
1616
<button onclick="alert(document.documentElement.clientHeight)">alert(document.documentElement.clientHeight)</button>
1717
```
1818

19-
````warn header="Not `window.innerWidth/Height`"
20-
Browsers also support properties `window.innerWidth/innerHeight`. They look like what we want. So why not to use them instead?
19+
````warn header="ليس `window.innerWidth / Height`"
20+
كما تدعم المستعرضات الخصائص `window.innerWidth / innerHeight`. يبدون مثل ما نريد. فلماذا لا تستخدمها بدلاً من ذلك؟
2121

22-
If there exists a scrollbar, and it occupies some space, `clientWidth/clientHeight` provide the width/height without it (subtract it). In other words, they return width/height of the visible part of the document, available for the content.
22+
إذا كان هناك شريط تمرير ، ويحتل بعض المساحة ، يوفر `clientWidth / clientHeight` العرض / الارتفاع بدونه (اطرحه). بمعنى آخر ، تقوم بإرجاع عرض / ارتفاع الجزء المرئي من المستند المتاح للمحتوى.
2323

24-
...And `window.innerWidth/innerHeight` include the scrollbar.
24+
... و `window.innerWidth / innerHeight` تتضمن شريط التمرير.
2525

26-
If there's a scrollbar, and it occupies some space, then these two lines show different values:
26+
إذا كان هناك شريط تمرير ، ويشغل بعض المساحة ، فإن هذين الخطين يعرضان قيمًا مختلفة:
2727
```js run
2828
alert( window.innerWidth ); // full window width
2929
alert( document.documentElement.clientWidth ); // window width minus the scrollbar
3030
```
3131

32-
In most cases we need the *available* window width: to draw or position something. That is: inside scrollbars if there are any. So we should use `documentElement.clientHeight/Width`.
33-
````
32+
في معظم الحالات ، نحتاج إلى عرض النافذة * المتوفرة *: لرسم شيء ما أو وضعه. هذا هو: داخل أشرطة التمرير إذا كان هناك أي. لذا يجب علينا استخدام `documentElement.clientHeight / Width`.
33+
`` ``
3434

35-
```warn header="`DOCTYPE` is important"
36-
Please note: top-level geometry properties may work a little bit differently when there's no `<!DOCTYPE HTML>` in HTML. Odd things are possible.
35+
`` `warn header =" "DOCTYPE` مهم"
36+
يرجى ملاحظة: قد تعمل خصائص الهندسة عالية المستوى بشكل مختلف قليلاً عندما لا يكون هناك <! DOCTYPE HTML> `في HTML. الأشياء الغريبة ممكنة.
3737

38-
In modern HTML we should always write `DOCTYPE`.
39-
```
38+
في HTML الحديثة يجب أن نكتب دائمًا "DOCTYPE".
39+
``
4040

41-
## Width/height of the document
41+
## عرض / ارتفاع الوثيقة
4242

43-
Theoretically, as the root document element is `document.documentElement`, and it encloses all the content, we could measure document full size as `document.documentElement.scrollWidth/scrollHeight`.
43+
نظريًا ، نظرًا لأن عنصر المستند الجذر هو `document.documentElement` ، ويشتمل على كل المحتوى ، يمكننا قياس حجم المستند بالكامل على أنه` document.documentElement.scrollWidth / ScrollHeight`.
4444

45-
But on that element, for the whole page, these properties do not work as intended. In Chrome/Safari/Opera if there's no scroll, then `documentElement.scrollHeight` may be even less than `documentElement.clientHeight`! Sounds like a nonsense, weird, right?
45+
ولكن في هذا العنصر ، بالنسبة للصفحة بأكملها ، لا تعمل هذه الخصائص على النحو المنشود. في Chrome / Safari / Opera إذا لم يكن هناك تمرير ، فقد يكون `documentElement.scrollHeight` أقل من` documentElement.clientHeight`! يبدو هراء ، غريب ، أليس كذلك؟
4646

47-
To reliably obtain the full document height, we should take the maximum of these properties:
47+
للحصول على ارتفاع المستند بشكل موثوق ، يجب أن نأخذ الحد الأقصى من هذه الخصائص:
4848

4949
```js run
5050
let scrollHeight = Math.max(
@@ -56,101 +56,100 @@ let scrollHeight = Math.max(
5656
alert('Full document height, with scrolled out part: ' + scrollHeight);
5757
```
5858

59-
Why so? Better don't ask. These inconsistencies come from ancient times, not a "smart" logic.
59+
لما ذلك؟ من الأفضل ألا تسأل. هذه التناقضات تأتي من العصور القديمة ، وليس منطق "ذكي".
6060

61-
## Get the current scroll [#page-scroll]
61+
## احصل على التمرير الحالي [# page-roll]
6262

63-
DOM elements have their current scroll state in `elem.scrollLeft/scrollTop`.
63+
عناصر DOM لها حالة التمرير الحالية في `elem.scrollLeft / rollTop`.
6464

65-
For document scroll `document.documentElement.scrollLeft/Top` works in most browsers, except older WebKit-based ones, like Safari (bug [5991](https://bugs.webkit.org/show_bug.cgi?id=5991)), where we should use `document.body` instead of `document.documentElement`.
65+
بالنسبة إلى تمرير المستند ، يعمل المستند document.documentElement.scrollLeft / Top` في معظم المتصفحات ، باستثناء المتصفحات القديمة التي تستند إلى WebKit ، مثل Safari (الخطأ [5991] (https://bugs.webkit.org/show_bug.cgi؟id=5991) ) ، حيث يجب أن نستخدم `document.body` بدلاً من` document.documentElement`.
6666

67-
Luckily, we don't have to remember these peculiarities at all, because the scroll is available in the special properties `window.pageXOffset/pageYOffset`:
67+
لحسن الحظ ، ليس علينا أن نتذكر هذه الخصائص على الإطلاق ، لأن التمرير متاح في الخصائص الخاصة `window.pageXOffset/pageYOffset`:
6868

6969
```js run
7070
alert('Current scroll from the top: ' + window.pageYOffset);
7171
alert('Current scroll from the left: ' + window.pageXOffset);
7272
```
7373

74-
These properties are read-only.
75-
76-
## Scrolling: scrollTo, scrollBy, scrollIntoView [#window-scroll]
74+
هذه الخصائص للقراءة فقط.
7775

78-
```warn
79-
To scroll the page from JavaScript, its DOM must be fully built.
76+
## التمرير: التمرير إلى التمرير التمرير التمرير العرضي [# window-تمرير]
8077

81-
For instance, if we try to scroll the page from the script in `<head>`, it won't work.
82-
```
78+
احذر
79+
لتمرير الصفحة من JavaScript ، يجب بناء DOM بالكامل.
8380

84-
Regular elements can be scrolled by changing `scrollTop/scrollLeft`.
81+
على سبيل المثال ، إذا حاولنا تمرير الصفحة من البرنامج النصي في `<head>` ، فلن تعمل.
82+
``
8583

86-
We can do the same for the page using `document.documentElement.scrollTop/Left` (except Safari, where `document.body.scrollTop/Left` should be used instead).
84+
يمكن تمرير العناصر العادية عن طريق تغيير `التمرير / التمرير لليسار`.
8785

88-
Alternatively, there's a simpler, universal solution: special methods [window.scrollBy(x,y)](mdn:api/Window/scrollBy) and [window.scrollTo(pageX,pageY)](mdn:api/Window/scrollTo).
86+
يمكننا فعل الشيء نفسه للصفحة باستخدام `document.documentElement.scrollTop / Left` (باستثناء Safari ، حيث يجب استخدام` document.body.scrollTop / Left` بدلاً من ذلك).
8987

90-
- The method `scrollBy(x,y)` scrolls the page *relative to its current position*. For instance, `scrollBy(0,10)` scrolls the page `10px` down.
88+
بدلاً من ذلك ، هناك حل أبسط وعالمي: طرق خاصة [window.scrollBy (x، y)] (mdn: api / Window / rollBy) و [window.scrollTo (pageX، pageY)] (mdn: api / Window / rollTo) .
9189

92-
```online
93-
The button below demonstrates this:
90+
- طريقة `التمرير (x، y) 'تقوم بتمرير الصفحة * بالنسبة إلى موقعها الحالي *. على سبيل المثال ، يؤدي `التمرير (0،10)` إلى تمرير الصفحة `10 بكسل` لأسفل.
9491

95-
<button onclick="window.scrollBy(0,10)">window.scrollBy(0,10)</button>
96-
```
97-
- The method `scrollTo(pageX,pageY)` scrolls the page *to absolute coordinates*, so that the top-left corner of the visible part has coordinates `(pageX, pageY)` relative to the document's top-left corner. It's like setting `scrollLeft/scrollTop`.
92+
`` online
93+
يوضح الزر أدناه هذا:
9894

99-
To scroll to the very beginning, we can use `scrollTo(0,0)`.
95+
<button onclick = "window.scrollBy (0،10)"> window.scrollBy (0،10) </button>
96+
``
97+
- طريقة `التمرير (pageX ، pageY)` تقوم بتمرير الصفحة * إلى إحداثيات مطلقة * ، بحيث يكون للزاوية العلوية اليسرى للجزء المرئي إحداثيات `((pageX ، pageY)` بالنسبة إلى الزاوية العلوية اليسرى للمستند. الأمر أشبه بإعداد `التمرير الأيسر / التمرير العلوي`.
10098

99+
للتمرير إلى البداية ، يمكننا استخدام `التمرير (0،0)`.
101100
```online
102101
<button onclick="window.scrollTo(0,0)">window.scrollTo(0,0)</button>
103102
```
104103
105-
These methods work for all browsers the same way.
104+
تعمل هذه الطرق لجميع المتصفحات بنفس الطريقة.
106105
107106
## scrollIntoView
108107
109-
For completeness, let's cover one more method: [elem.scrollIntoView(top)](mdn:api/Element/scrollIntoView).
108+
للاستكمال ، دعنا نغطي طريقة أخرى: [elem.scrollIntoView (top)] (mdn: api / Element / rollIntoView).
110109
111-
The call to `elem.scrollIntoView(top)` scrolls the page to make `elem` visible. It has one argument:
110+
يؤدي استدعاء `elem.scrollIntoView (أعلى)` إلى تمرير الصفحة لإظهار `elem`. لها حجة واحدة:
112111
113-
- if `top=true` (that's the default), then the page will be scrolled to make `elem` appear on the top of the window. The upper edge of the element is aligned with the window top.
114-
- if `top=false`, then the page scrolls to make `elem` appear at the bottom. The bottom edge of the element is aligned with the window bottom.
112+
- إذا كان `top = true` (هذا هو الافتراضي) ، فسيتم تمرير الصفحة لإظهار` elem` في أعلى النافذة. يتم محاذاة الحافة العلوية للعنصر مع أعلى النافذة.
113+
- إذا كان `top = false` ، فتمرر الصفحة لتظهر` elem` في الأسفل. يتم محاذاة الحافة السفلية للعنصر مع أسفل النافذة.
115114
116115
```online
117-
The button below scrolls the page to make itself show at the window top:
116+
يمرر الزر أدناه الصفحة لإظهار نفسها في أعلى النافذة:
118117
119-
<button onclick="this.scrollIntoView()">this.scrollIntoView()</button>
118+
<button onclick = "this.scrollIntoView ()"> this.scrollIntoView () </button>
120119
121-
And this button scrolls the page to show it at the bottom:
120+
وهذا الزر يقوم بتمرير الصفحة لإظهارها في الأسفل:
122121
123122
<button onclick="this.scrollIntoView(false)">this.scrollIntoView(false)</button>
124123
```
125124

126-
## Forbid the scrolling
125+
## منع التمرير
127126

128-
Sometimes we need to make the document "unscrollable". For instance, when we need to cover it with a large message requiring immediate attention, and we want the visitor to interact with that message, not with the document.
127+
نحتاج أحيانًا إلى جعل المستند "غير قابل للتمرير". على سبيل المثال ، عندما نحتاج إلى تغطيتها برسالة كبيرة تتطلب اهتمامًا فوريًا ، ونريد من الزائر أن يتفاعل مع هذه الرسالة ، وليس مع المستند.
129128

130-
To make the document unscrollable, it's enough to set `document.body.style.overflow = "hidden"`. The page will freeze on its current scroll.
129+
لجعل المستند غير قابل للتمرير ، يكفي تعيين `document.body.style.overflow =" hidden "`. سيتم تجميد الصفحة في التمرير الحالي.
131130

132131
```online
133-
Try it:
132+
جربها:
134133
135134
<button onclick="document.body.style.overflow = 'hidden'">document.body.style.overflow = 'hidden'</button>
136135
137136
<button onclick="document.body.style.overflow = ''">document.body.style.overflow = ''</button>
138137
139-
The first button freezes the scroll, the second one resumes it.
140-
```
138+
يجمد الزر الأول التمرير ، ويستأنفه الزر الثاني.
139+
``
141140
142-
We can use the same technique to "freeze" the scroll for other elements, not just for `document.body`.
141+
يمكننا استخدام نفس التقنية "لتجميد" التمرير للعناصر الأخرى ، وليس فقط لـ `المستند.
143142
144-
The drawback of the method is that the scrollbar disappears. If it occupied some space, then that space is now free, and the content "jumps" to fill it.
143+
عيب الطريقة هو اختفاء شريط التمرير. إذا كانت تحتل بعض المساحة ، فإن هذه المساحة الآن خالية ، ويزداد المحتوى لملئها.
145144
146-
That looks a bit odd, but can be worked around if we compare `clientWidth` before and after the freeze, and if it increased (the scrollbar disappeared) then add `padding` to `document.body` in place of the scrollbar, to keep the content width the same.
145+
يبدو هذا غريبًا بعض الشيء ، ولكن يمكن التعامل معه إذا قارنا `ClientWidth` قبل التجميد وبعده ، وإذا زاد (اختفى شريط التمرير) ، فأضف` padding` إلى `document.body` بدلاً من شريط التمرير ، إلى حافظ على عرض المحتوى كما هو.
147146
148-
## Summary
147+
## الملخص
149148
150-
Geometry:
149+
الهندسة:
151150
152-
- Width/height of the visible part of the document (content area width/height): `document.documentElement.clientWidth/Height`
153-
- Width/height of the whole document, with the scrolled out part:
151+
- عرض / ارتفاع الجزء المرئي من المستند (عرض / ارتفاع منطقة المحتوى): `document.documentElement.clientWidth / Height`
152+
- عرض / ارتفاع الوثيقة بأكملها ، مع الجزء الممرر:
154153
155154
```js
156155
let scrollHeight = Math.max(
@@ -160,11 +159,11 @@ Geometry:
160159
);
161160
```
162161
163-
Scrolling:
162+
التمرير:
164163
165-
- Read the current scroll: `window.pageYOffset/pageXOffset`.
166-
- Change the current scroll:
164+
- قراءة التمرير الحالي: `window.pageYOffset / pageXOffset`.
165+
- تغيير التمرير الحالي:
167166
168-
- `window.scrollTo(pageX,pageY)` -- absolute coordinates,
169-
- `window.scrollBy(x,y)` -- scroll relative the current place,
170-
- `elem.scrollIntoView(top)` -- scroll to make `elem` visible (align with the top/bottom of the window).
167+
- `window.scrollTo (pageX ، pageY)` - الإحداثيات المطلقة ،
168+
- `window.scrollBy (x، y)` - انتقل نسبيًا إلى المكان الحالي ،
169+
- `elem.scrollIntoView (أعلى)` - قم بالتمرير لجعل `elem` مرئيًا (محاذاة مع الجزء العلوي / السفلي من النافذة).

0 commit comments

Comments
 (0)